What is the advantage of using header-catname.php over is_category(‘catname’);?

Let’s say I have a category named as catname (the slug would be catname). I want the header styled differently when visitors open the category catname.

The article on Ghacks explains how to create a custom header (styled differently) when visitors open the category catname, achieved by creating header-catname.php and including it in category-catname.php with include(TEMPLATEPATH.'/header-catname.php'); (instead of the usual get_header(); ). You can read the short article itself if my explanations is difficult to follow (I apologize for my lack of proficient English).

Read More

My question is: what is the advantage of using above method, over using is_category ('catname') in header.php to create custom header only for the catname category?

I mean, in the header.php we could just use this without creating additional header-catname.php:

 <?php if ( is_category('catname') ) { ?>

 /* Insert custom header for category catname here */

 <?php } else { ?>

 /* Insert the usual header here */

 <?php } ?>

Does using the conditional IF make the page load slower? Or is there any disadvantage?

Related posts

Leave a Reply

2 comments

  1. The article on Ghacks is actually a pretty silly way to do it as well.

    The get_header() function is actually a pretty smart function. You can do some neat things with it. For example, you can do this:

    get_header('category');

    That will cause it to load the header-category.php file, if such a file exists, or the header.php file, if header-category.php does not exist.

    Here’s a cool way to make use of that property:

    if (is_category()) {
      $cat = get_queried_object();
      get_header($cat->name);
    }
    

    This will cause it to load header-catname.php, using the actual category name, with a fallback to header.php if such a file does not exist.

    If you want, you can even extend that to be more generic and work for any taxonomy:

    if ( is_category() || is_tag() || is_tax() ) {
      $term = get_queried_object();
      get_header($term->name);
    }
    

    Lots of neat things you can do with those template fallbacks.

  2. Both approaches a almost equal. Access to the file system is always slower than plain PHP execution in a file.

    If your extra code is short put it into the same file. If it rather long separate it to keep the default code readable. Execution time is not the only, and often not the most important, criterion. Maintainability counts too, especially when you have to fix something really fast.

    A real advantage of separate files: A child theme can override just that specific file.