Making WordPress More Modular with Multiple Sidebars, Headers, and Footers

October 22, 2009  | 
12 Comments

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.

Featured Plugin - Send beautiful html email newsletters, from WordPress!

Now there's no need to pay for a third party service to sign up, manage and send beautiful email newsletters to your subscriber base - this plugin has got the lot.
Find out more

Featured Plugin - Start your own Quora / StackOverflow / Yahoo Q&A site

It's now incredibly easy to start your own Q&A site using nothing more than WordPress - The Q&A plugin simply and brilliantly transforms any site, or page, into a perfect support or Q&A environment.
Find out more

Featured Plugin - Turn any WordPress page into a fully featured wiki!

To get a wiki up and running you used to need to install Mediawiki and toil away for days configuring it... not any more! This plugin gives you *all* the functionality you want from a wiki, in WordPress!!!
Find out more

Featured Plugin - Add bottom corner (or anywhere else) chat to your site

No javascript required, no third part chat engine, just fully featured chat right in your own database on your own WP sites - couldn't be easier.
Find out more

Featured Plugin - Open an Online Store with MarketPress

Out of all the WordPress ecommerce plugins available, this has got to be the winner - easy to configure, powerful functionality, multiple gateways and more. A simply brilliant plugin!
Find out more

Featured Plugin - WordPress + Google Maps = Perfect

Simply insert google maps into posts, sidebars and pages - show directions, streetview, provide image overlays and do it all from a simple button and comprehensive widget.
Find out more

Featured Plugin - Easily integrate your WordPress site with Facebook

Would you like to add Facebook comments, registration, 'Like' buttons and autoposting to your WP site? Well, The Ultimate Facebook plugin has got that all covered!
Find out more

Featured Plugin - Host sites, get paid, just like WordPress.com

If you've ever wondered how you could offer a paid site management and hosting service, then this is the plugin for you. Offer a freemium or paid service, for any niche you like, it's powered Edublogs.org to success already!
Find out more

Featured Plugin - Start Your Own Powerful Membership Site

If you're thinking about starting a paid, or just private, membership site then this is truly the plugin you've been looking for. Easy to use, massively configurable and ready to go out of the box!
Find out more

12 Responses to Making WordPress More Modular with Multiple Sidebars, Headers, and Footers

  1. 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?

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

  3. On the sidebars, I would like to post a picture of the item next to the title of that page. Is that possible?

  4. What item are you referring to? I’m sure it’s possible ;) Can you give me any more details?

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

  6. 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!

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

  8. Got it now – I was editing the text in footer.php as apposed to index.php.

    Thanks for the tip – exactly what i needed :)

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

  10. Hi there, I think I’m missing something. Do you create a new “header2.php” file? It doesn’t explain the step of making the different sidebar or header/footer. Thanks :)

  11. Thanks for this tutorial.

    What I’m hoping to do is change my post page to a similar design as 500px.com. See this page for example: http://500px.com/photo/4274374?from=editors

    The sidebar (although I’m not sure it was created as a sidebar) on the right contains details and info specific to that page, yet has social networking links below it. I’m hoping to to display details in a similar manner on the top, and then include my widgets (social networking, categories etc) below it. Any idea how to do that?

    Thanks.

Click on a tab to select how you'd like to leave your comment

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Subscribe without commenting