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.