What difference does it make if I use index.php as HTML wireframe versus writing each main template file as a full HTML document?

Option 2 is the best option. To know why, one needs to look at the template loader.

(The template hierarchy means nothing, if you do not know how it really works or come from)

    if ( defined('WP_USE_THEMES') && WP_USE_THEMES ) :
59          $template = false;
60          if     ( is_404()            && $template = get_404_template()            ) :
61          elseif ( is_search()         && $template = get_search_template()         ) :
62          elseif ( is_front_page()     && $template = get_front_page_template()     ) :
63          elseif ( is_home()           && $template = get_home_template()           ) :
64          elseif ( is_post_type_archive() && $template = get_post_type_archive_template() ) :
65          elseif ( is_tax()            && $template = get_taxonomy_template()       ) :
66          elseif ( is_attachment()     && $template = get_attachment_template()     ) :
67                  remove_filter('the_content', 'prepend_attachment');
68          elseif ( is_single()         && $template = get_single_template()         ) :
69          elseif ( is_page()           && $template = get_page_template()           ) :
70          elseif ( is_singular()       && $template = get_singular_template()       ) :
71          elseif ( is_category()       && $template = get_category_template()       ) :
72          elseif ( is_tag()            && $template = get_tag_template()            ) :
73          elseif ( is_author()         && $template = get_author_template()         ) :
74          elseif ( is_date()           && $template = get_date_template()           ) :
75          elseif ( is_archive()        && $template = get_archive_template()        ) :
76          elseif ( is_comments_popup() && $template = get_comments_popup_template() ) :
77          elseif ( is_paged()          && $template = get_paged_template()          ) :
78          else :
79                  $template = get_index_template();
80          endif;

This template loader runs on each and every page load on the front end regardless. As you can see, WordPress goes through a list and validates the conditions against the page being requested. Lets use a category page as example with the category being the Uncategorized category.

An if/else statement executes the first condition that returns true, and on category pages it will be the is_category() conditional statement. This statement executes get_category_template() and also valuates the statement. If this statement also ring true (a valid category page is available), the template is loaded accordingly to what was found, if this statement return a false, the template loader carries on to find the next true statement which I will tackle a bit later.

What happens inside get_category_template() is important. The function creates three template names according to the category page being requested. As example, the following is created in order, category-uncategorized.php, category-1.php and category.php. This template names are stored in an array and passed to get_query_template(). This is the function that has the task to search for the template names passed to it and to return the first template that exists and are available. This is done with locate_template().

So what this is saying is, WordPress (actually locate_template() if you want to get technical) starts by looking for category-uncategorized.php, if it is found, the template is returned and loaded. If not found, WordPress moves on tries category-1.php and if that fails it lastly tries category.php. If none are found, as I said, our

elseif ( is_category()       && $template = get_category_template()  

condition returns false and the conditions further down are evaluated. The next condition that rings true will be is_archive() (remember, is_archive() returns true on category, tag, taxonomy, custom post type archives, author and date related pages). The same process as above happens but this time with archive.php being looked for and loaded if it exists. If archive.php does not exist, the whole conditional statements valuates to it’s default which loads index.php through get_index_template(). This is the ultimate fallback for every page

CONCLUSION AND FINAL ANSWER

Even though you might think that having category.php will result in a big overhead, it does not. By the time that index.php is loaded on your category page, (lets use our example again) for the uncategorized category, WordPress has already looked and tried to load category-uncategorized.php, category-1.php, category.php and archive.php. So go on and have your nice-to-have templates (all templates except index.php)(which is your OPTION 2). Having 10 nice-to-have templates is better than an index.php which is as long as the Nile river and have more logic crammed into it than what Albert Einstein had in his brain (which is your OPTION 1)

It is also very important, coming back to Option1 that you keep templates as short as possible, maintainable and readable. It is really of no use having a template, which might be 0.0000001 seconds faster, but is a total mess and a real nightmare to maintain.

At the end, option 2 has much much more pro’s than option 1.

Leave a Comment