Post Pic

How to: Get a particular header, footer, or sidebar

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

Related Posts

Related Posts

No related posts.

22 Responses

Jan 04 2009 13:37

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” ?

Jan 04 2009 14:01

@Mikael: Yes, this is a good start! Then you just have to edit the new files and make the desired modifications.

Jan 04 2009 19:36

Oh yes of course. I meant to say that as well but thanks for adding that.

Jan 04 2009 20:14

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!

Jan 04 2009 20:23

Glad it was helpful to you, Shawn!

Jan 04 2009 21:41

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 ?

Jan 04 2009 22:04

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

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

Jan 04 2009 22:08

Ah ! Got it. Needed the closing )

So it looks like this now (which works)

Jan 04 2009 22:10

That didn’t work (admin please delete my previous comments)

This is what I used and it worked.

Jan 04 2009 22:44

…But didn’t because it called the main header as well as the category one !

I give up. I’ll go away now.

Jan 05 2009 04:23

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
`

Jan 05 2009 04:23

Oh well, my code won’t pop up.

Jan 05 2009 13:03

It’s great to optimize specific post for SEO. I always do this in my blog.

Jan 05 2009 16:17

Yeah this a great trick

Jan 05 2009 22:18

Very Useful indeed.

Jan 25 2009 22:03

Where i put this link:

Feb 24 2009 20:47

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

Mar 11 2009 15:47

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?

Mar 20 2009 16:53

Thanks for sharing great recipe.

Mar 24 2009 04:19

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:

Leave a Comment

* Name, Email, Comment are Required