1

If the official themes and Underscores are some sort of a guide, one would notice that multiple files with little differences are preferred over conditional tags (archive.php, search.php, category.php, index.php etc.)

I find the existence of multiple similar files disorienting and I plan to combine some of them using conditional tags, since the main difference between them is the Title and description of the page. Could this for any reason be considered bad practice?

And if both ways are valid, would there be any impact on the load speed? Which one would be faster?

1

Before I start, and before you continue reading the rest of the post, make sure to check my answer regarding templates in WordPress

Now that you understand that all templates, except index.php, are only nice-to-have templates, lets continue.

....notice that multiple files with little differences are preferred over conditional tags

Templates are already accounted for through the template hierarchy. Simply including a template like category.php does not require any extra work. To really understand this, lets look at the template loader (which also included the template_include filter to later the template hierarchy as needed)

44  if ( defined('WP_USE_THEMES') && WP_USE_THEMES ) :
45          $template = false;
46          if     ( is_404()            && $template = get_404_template()            ) :
47          elseif ( is_search()         && $template = get_search_template()         ) :
48          elseif ( is_front_page()     && $template = get_front_page_template()     ) :
49          elseif ( is_home()           && $template = get_home_template()           ) :
50          elseif ( is_post_type_archive() && $template = get_post_type_archive_template() ) :
51          elseif ( is_tax()            && $template = get_taxonomy_template()       ) :
52          elseif ( is_attachment()     && $template = get_attachment_template()     ) :
53                  remove_filter('the_content', 'prepend_attachment');
54          elseif ( is_single()         && $template = get_single_template()         ) :
55          elseif ( is_page()           && $template = get_page_template()           ) :
56          elseif ( is_singular()       && $template = get_singular_template()       ) :
57          elseif ( is_category()       && $template = get_category_template()       ) :
58          elseif ( is_tag()            && $template = get_tag_template()            ) :
59          elseif ( is_author()         && $template = get_author_template()         ) :
60          elseif ( is_date()           && $template = get_date_template()           ) :
61          elseif ( is_archive()        && $template = get_archive_template()        ) :
62          elseif ( is_comments_popup() && $template = get_comments_popup_template() ) :
63          elseif ( is_paged()          && $template = get_paged_template()          ) :
64          else :
65                  $template = get_index_template();
66          endif;
67          /**
68           * Filter the path of the current template before including it.
69           *
70           * @since 3.0.0
71           *
72           * @param string $template The path of the template to include.
73           */
74          if ( $template = apply_filters( 'template_include', $template ) )
75                  include( $template );
76          return;
77  endif;

Lets look at how templates are loaded for category pages. As you can see, when we visit a category page, the template which should be loaded is decided by the following line

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

Obviously because is_category() is the only condition that will return true in the conditional tree. What template to load exactly when a category page is viewed is partly decided in the get_category_template() function

function get_category_template() {
    $category = get_queried_object();

    $templates = array();

    if ( ! empty( $category->slug ) ) {
        $templates[] = "category-{$category->slug}.php";
        $templates[] = "category-{$category->term_id}.php";
    }
    $templates[] = 'category.php';

    return get_query_template( 'category', $templates );
}

Lets say, we are viewing the Cars category, the slug being cars and category id is 10. get_category_template() builds the following templates to search for

  • category-cars.php

  • category-10.php

  • category.php

This is how the array looks like: (in order of importance/hierarchy)

array(3) {
  [0]=>
  string(17) "category-cars.php"
  [1]=>
  string(15) "category-10.php"
  [2]=>
  string(12) "category.php"
}

This is what is passed to get_query_template().

function get_query_template( $type, $templates = array() ) {
    $type = preg_replace( '|[^a-z0-9-]+|', '', $type );

    if ( empty( $templates ) )
        $templates = array("{$type}.php");

    $template = locate_template( $templates );

    /**
     * Filter the path of the queried template by type.
     *
     * The dynamic portion of the hook name, `$type`, refers to the filename -- minus the file
     * extension and any non-alphanumeric characters delimiting words -- of the file to load.
     * This hook also applies to various types of files loaded as part of the Template Hierarchy.
     *
     * @since 1.5.0
     *
     * @param string $template Path to the template. See locate_template().
     */
    return apply_filters( "{$type}_template", $template );
}

The real decision maker in all of this is the get_query_template() which uses locate_template to check which is the first template available in the hierarchy and then loads it. So, first get_query_template() will look if category-cars.php exists, if not, it will look for category-10.php and lastly for category.php if none of the other templates exists. The first one that is found is returned and loaded. If none of these templates are found, get_query_template() returns false, and no template is set for loading

If you go back to this line

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

Because $template = get_category_template() returned false, the condition fails, and only then the last condition returns true

65   $template = get_index_template();

This is our final fallback, which in essence returns and loads index.php

As you can see, if you only have index.php in your theme, or a template for each and every category, WordPress does all the job above on every page load regardless. So there really is nothing gained or lost by having only index.php or a category page for every category.

Loading time will slightly (really irrelevant) be effected (we are talking extremely small increases here) with having more templates as the template loader will have a bit more templates to go through, but this should never be an issue (except if you have hundreds of templates)

...using conditional tags.... Could this for any reason be considered bad practice?

As I stated in the linked answer, you only really need index.php to display any page on the site, any other template are there for convenience. You can have only index.php and use conditional tags to target specific page types if you wish.

There is really no standard regarding this issue, it is really user preference, and readability and maintainability of templates. If you only need to change the title between pages, you can just use conditionals in index.php, if you need more changes, it would be better to create a specific template for the specific page. It is really no use having just index.php with hundreds of conditionals and a few hundred lines of code. Such a file would be really messy, and might take longer to load due to hundreds of conditions that needs to be checked

And if both ways are valid, would there be any impact on the load speed? Which one would be faster?

Page speed should really not be an issue here as I have shown in everything preceding this. Choosing either of the two options might or might not have any impact on page load times. Even if there are an impact on page load times, the difference will almost be non measurable.

What really should be deciding factor here would be maintainability and usability of the template/templates, how readable those templates are and overall user friendliness of the theme, and what your preference is and what you are comfortable with.

I hope this shines some light on the whole issue

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.