Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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).

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?

share|improve this question
    
Why is my question downvoted? :/ I don't think it's similar to wordpress.stackexchange.com/questions/5194/… , because I'm comparing it with the IF conditional. :/ –  deathlock Sep 21 '12 at 22:48

2 Answers 2

up vote 7 down vote accepted

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.

share|improve this answer
    
Whoa interesting, I didn't know about get_header('category'), seems to be lot more effective thant the way provided in Ghacks. I assume this works with get_footer, get_sidebar,etc too? –  deathlock Sep 22 '12 at 15:54
    
Yup, footer and sidebar work the same way. –  Otto Sep 22 '12 at 16:10
    
Is there any differences in speed or execution process between using the get_header('catname'); and include(TEMPLATEPATH.'/header-catname.php');? –  deathlock Sep 22 '12 at 17:50
    
Not really, no. They basically do the same thing, but get_header is more future-proof. –  Otto Sep 22 '12 at 19:01
    
I see. Thanks for the answer! Would like to upvote both answers but still not enough point for now. :) –  deathlock Sep 22 '12 at 21:10

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.

share|improve this answer
    
I see... is there any other advantage besides maintainability? –  deathlock Sep 22 '12 at 15:52
    
See my edit (last sentence). –  toscho Sep 22 '12 at 15:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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