
Since WordPress 2.7, it is easier to choose which header, footer or sidebar file you want to include on your theme. In this recipe, I’ll show you how to do.

Since WordPress 2.7, it is easier to choose which header, footer or sidebar file you want to include on your theme. In this recipe, I’ll show you how to do.
The following code will check if the reader is on the "WordPress" category. If yes, header-wordpress.php will be inclued, otherwise the default header will be used:
<?php if is_category('WordPress') {
get_header('wordpress');
} else {
get_header();
} ?>
You can also use that new functionality with footers and sidebars:
<?php get_footer('myfooter'); ?>
will include footer-myfooter.php
<?php get_sidebar('mysidebar'); ?>
will include sidebar-mysidebar.php
22 Responses
So what is the best way to create additional headers and footers? Do you just make copies of the original ones and name them “newname.php” ?
@Mikael: Yes, this is a good start! Then you just have to edit the new files and make the desired modifications.
Oh yes of course. I meant to say that as well but thanks for adding that.
Well that’s just great! I just finished modifying about 8 page templates to get this same effect (replacing the sidebar for certain pages). Shoulda known that as soon as I got something figured out the hard way someone would come along and make it easy. Thanks for the tip!
Glad it was helpful to you, Shawn!
Apologies for the silly question, but when trying the header code I get this:
Parse error: syntax error, unexpected T_STRING, expecting '(' in /home/ ... /index.php on line 1
What gives ?
Update – I’ve realized that I should read what the error message was telling me. I’ve added a ( before the is so now it looks like:
<?php if (is_category ..etcWhich cured that, but didn’t fix the error.
Now I have a new one:
Parse error: syntax error, unexpected '{' in /home/Back to the drawing board.
Ah ! Got it. Needed the closing )
So it looks like this now (which works)
That didn’t work (admin please delete my previous comments)
This is what I used and it worked.
…But didn’t because it called the main header as well as the category one !
I give up. I’ll go away now.
I easily get confused by that code, I usually do this one when I want to call a different header sidebar or footer (which I am using on my site to have different sidebars on posts, the blog page and none on the frontpage)
like for exmpla the blog page, when I would want to call a sidebar, I’d use
“
for the single post page it would be something like
`
Oh well, my code won’t pop up.
It’s great to optimize specific post for SEO. I always do this in my blog.
Yeah this a great trick
Very Useful indeed.
Where i put this link:
Thanks a lot for those infos. It works fine.
But my problem is that my theme GETS LOADED TWICE on a page that uses a header that is not the default one.
Thanks for your help
I would like to know How to add another sidebar into the dropdown in the Appearance>Widgets page in WP-Admin. And then how to specify use of these sidebars on different pages or posts.
Anyone?
Thanks for sharing great recipe.
Your blog is really so full of so many helpful and hands-on tips, I wished I had found it sooner, as I have really struggled with these type of code issues with 4 blogs.
Thanks
Trackbacks: