You’ve found the perfect theme for your WordPress installation. It already has most of what you need, except you’d like to include a different sidebar on some of the pages. Or perhaps you would like to have a certain sidebar included based on the category of posts you are displaying. Now there’s an easy way to do this using custom fields. Much thanks to Jean-Baptiste Jung over at WP Recipes for his helpful post. Changing the sidebar based on a custom field is especially helpful if you’re a developer delivering multiple templates to accommodate a client who wants to make his own updates. Creating content with a more modular layout is much simpler when using custom fields.
Here’s the How To:
1. Open single.php* and find the call to the function:
<?php get_sidebar(); ?>
2. Replace that with:
<?php $sidebar = get_post_meta($post->ID, "sidebar", true); get_sidebar($sidebar); ?>
3. Whenever you create a new post, you will be able to include the alternate sidebar by creating a custom field called “sidebar” with a value of “2”. WordPress will then automatically include sidebar-2.php instead of the default sidebar. So easy!
*You can also do this on page.php.
You can even have multiples for header.php and footer.php by using the following:
For header:
<?php $header = get_post_meta($post->ID, "header", true); get_header($header); ?>
For footer:
<?php $footer = get_post_meta($post->ID, "footer", true); get_footer($footer); ?>
The idea is to use your space more efficiently and to target your information to the right page or post, instead of having to settle with one default sidebar/header/footer. This makes your site more interesting for the users as well as an opportunity to include more rich content which will boost your SEO. Creating a more modular layout also enables you to build pages that are more CMS oriented with less of a blog style feel.
Innovative Uses for Multiple Sidebars/ Headers/ Footers Include:
1. Create more space for advertising
2. Display more pertinent content
3. Customize user profile templates in community sites based on user type.
4. Feature more users, groups, discussion topics, based on the selected category
5. Get niche specific for your users with selected RSS feeds or API’s from social networks
6. Drive traffic to key pages, categories, or posts
7. Include sign-up forms on certain page headers or sidebars
8. Generate and customize user specific headers for password protected posts or pages
The possibilities are endless! Subscribe now for more tips on bending your WordPress installation to your needs.







Sweet, thanks for sharing this. I guess all those sweet custom page templates with unique headers and sidebars, are pretty legacy these days.
I’m curious, though, what happens on a tag page, when posts with different sidebars are displayed. Does it use the CF specified in the most recently published post? Would that mean that if someone clicked on “Older Entries” that the sidebar, header or footer might change on them?
Thanks, Russell!
The page will use whatever is specified in the template, depending on if it’s a regular page, single post, or archives page, etc. It will always use the default header, siderbar, footer unless you specify otherwise in the template. If you create a new page that uses your archives template, then whatever you specify for that specific page (using the technique above) will be the sidebar, footer, header, etc.
On the sidebars, I would like to post a picture of the item next to the title of that page. Is that possible?
What item are you referring to? I’m sure it’s possible ;) Can you give me any more details?
I do not how to explain it more clearly. On the sidebar there is text on all the links and i want to add a photo
Hey jckeeler – Here is a plug in that might help you add photos to the links in your sidebar – assuming they are categories …
http://wordpress.org/extend/plugins/category-icons/
Check that out and see if it might be what you’re looking for. Your question is not exactly related to this post, but I found that plugin and thought it might be something for you. Good luck!
Hi Sarah,
Great tutorial – I seem to be having a little problem with the footer though. My site (www.freebiejeebie.co.uk) is fine with the new footer being there, but when you go to the section I have activated the new code I get the following results:
http://www.freebiejeebie.co.uk/category/Terminology/
As you can see there are 2 footers printed which is obiously not what I wanted :)
Any views as to why this would happen would be greatly appreciated.
Cheers
Stu
Got it now – I was editing the text in footer.php as apposed to index.php.
Thanks for the tip – exactly what i needed :)
Thankyou!!
If you knew how long I had searched to find this – I even installed a plugin that supposedly did this (it didn’t). Such a simple solution.