7

Assuming a website where page header and footer elements remain constant in every view: Due to WP's template hierarchy system, I could hard-code the general website layout in index.php. Then, I could use conditional tags in index.php to determine which inner content templates to render.

On the other hand, I could write the basic layout in each of the main templates repeatedly. Thus, I would write some more lines of code and introduce slightly more redundancy. However, maybe I save some file I/O operations, if, say, single.php can be rendered completely without having to fall back to index.php?

Option 1 without single.php, category.php, archive.php and so forth but with content-post.php, content-category.php ...

<?php
/**
 * The index.php file
 *
 */
get_header();
get_template_part('partials/layout-block-01');
get_template_part('partials/layout-block-02');
if (have_posts()) : 
  if (is_front_page()){
    ... // some front page specific markup and settings
  }
  elseif(is_category()){
    ... // some category specific markup and settings
  }
  elseif (is_singular()) {
    ... // some singular post specific markup and settings
  }
  elseif (is_archive()) {
    ... // some general archive specific markup and settings
  }
  else{
    ... // defaults
  }
  while (have_posts()) : 
    the_post();
    get_template_part( 'content', get_post_format());
  endwhile;
endif;
get_footer();
?>

Option 2: Each main template file contains the whole lot, like so

<?php
/**
 * The single.php file
 *
 */
get_header();
get_template_part('partials/layout-block-01');
get_template_part('partials/layout-block-02');
if (have_posts()) : 
  while (have_posts()) : 
    the_post();
    ... // single post markup
  endwhile;
endif;
get_footer();
?>

And another main template file

<?php
/**
 * The category.php file
 *
 */
get_header();
get_template_part('partials/layout-block-01');
get_template_part('partials/layout-block-02');
if (have_posts()) : 
  while (have_posts()) : 
    the_post();
    ... // category archive markup
  endwhile;
endif;
get_footer();
?>

So, are there any performance issues involved in either option? Is it just a matter of individual taste?

5

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.

  • If I ever have to create a pure index.php theme, then it will be tempting to call it nilestein ;-) – birgire Jan 16 '16 at 10:34
  • @birgire Hahaha, that would be the correct name for such a theme – Pieter Goosen Jan 16 '16 at 10:35
  • Once again Pieter, you saved me from getting grey hairs over this issue. I really appreciate your help and expertise. Which applies to all you other folks, too! Thank you. – Bunjip Jan 19 '16 at 20:05
  • My pleasure, glad you found this useful. Enjoy ;-) – Pieter Goosen Jan 20 '16 at 9:58
3

Saving file I/O operations.
You can save as much as possible on that level if you wish but in the end all will come on your coding/server configuration and so on.
The static/dynamic files can/would/should also be cached (possible on server level or by plugin/own code) and that also will help you having less worries if it is about minor things like files 'full with code' or amount of templates.

Template reducing
About your approach to reduce the use of templates, it is not always 'less is more'.
Here another example (also mentioned by ialocin):
Use a standard loop (for front-page.php/index.php/page.php/single.php and other templates of course)

<?php
if ( have_posts() ) : 

    // Start the Loop.
    while ( have_posts() ) : the_post();

    // Include format-specific template for the content.
    get_template_part( 'content', get_post_format() );

    // End the loop.
    endwhile;
else :
    // If no content, include the "No posts found" template part.
    get_template_part( 'content', 'none' );

endif;
?>

And use this example for content.php (which should be in the theme folder)

 <?php
 if ( is_front_page() ) { get_template_part( 'include/content', 'front' ); }
 if ( is_single() ) { get_template_part( 'include/content', 'single' ); }
 if ( is_page() ) { get_template_part( 'include/content', 'page' ); }
// and so on
?>

And (in the include folder) you can use files like content-front.php / content-single.php and so on.

These template_parts (which will be include in the loop, as mentioned in the above named files) contain the code you want to have for those template files.

For reference:

The loop
Template-hierarchy
Get template part
Organizing theme files
Page templates


This approach seems 'less sensitive' for a site when making just one little mistake. Because ppl will work mainly in the template parts(in that subfolder). Ofcourse all depends on the kind of mistake but I think you know what is meant.

Imho is it this way also faster/easier to trigger down errors and a little more easy-reference.

2

Short and painless, I would recommend going with Option 2.

Why? Because that is what the WordPress template hierarchy is there for. I can't see a reason from your question to differ from the template hierarchy concept.

Last but not least, in some projects it might be worth considering to even get the template files loop part via get_template_part().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.