Free Wordpress Blog Setup

Display Different Sidebar for Each Post and Page for WordPress

By Editorial Staff in Themes
Display Different Sidebar for Each Post and Page for WordPress

If you have been using WordPress, you probably have noticed that WordPress displays the same sidebar all across the site for most themes unless the designer himself created a custom homepage in which case the sidebar will be displayed on all pages but the homepage. In this article we will share a tutorial that will give you the option to choose a different sidebar to be displayed for each post and page for WordPress. This is an excellent snippet to bookmark for all WordPress theme designer and other users who would like to add this functionality.

This tutorial will combine the use of Custom Fields and the ability to call specific php templates. Therefore this tutorial has a prerequisite:

You must have basic knowledge of HTML and CSS in order for you to create a separate template.

You will need to open your single.php and/or page.php and find the following code:

<?php get_sidebar(); ?>

Replace the above code with this:

<?php $sidebar = get_post_meta($post->ID, "sidebar", true);
get_sidebar($sidebar);
?>

Now when you are writing a post or a page, and you want to display a custom sidebar all you need to do is add the custom field “Sidebar” and include the name of the sidebar file. For example if you insert “wpbpage”, it will display sidebar-wpbpage.php as your sidebar.

Custom Field for Specific Sidebar for Each Page or Post

You must have the file already in place to call it from a specific post. This trick is mostly used in larger magazine style sites, but it is commonly used for custom pages also. One of the easiest way to start out creating a new sidebar file is copying your sidebar.php codes into this new file and edit/add the codes that you want.

We will be covering a few more sidebar hacks, so stay updated if you want to know more about WordPress Sidebars.

Note: If you want to display this code outside the loop, then you need to see our tutorial on how to display custom fields outside the loop.

What Next?

Digg it
Save This Page
Subscribe to WPBeginner
Stumble it
Free Wordpress Blog Setup

Comments

29 Responses to “Display Different Sidebar for Each Post and Page for WordPress”
  1. Very nice tip and a clear explanation for setting this up. Thanks!

    Rob – LexiConn

  2. Alex says:

    I was looking for this tip a long time, THANK YOU!!!!

  3. Max Weaver says:

    This is a very useful tip. This blog is excellent. Every tip is so good I usually tweet it. I’ve built heaps of WordPress sites but have often been restricted by my lack of knowledge of PHP. Thanks for giving us these great tips. They are short, not too overly technical and well explained.

  4. Ben says:

    Really nice tipp, thanks for that! I was looking for such a simple solution for the last days and now found it here.

  5. Black Osama says:

    For newer versions of wordpress the code needs to be a little different. Got it working with the below code for 2.8.4
    ID, “sidebar”, true);
    include ($sidebar); ?>

  6. Scott says:

    Thanks for this info!

    I’m wondering if it’s possible to create a new sidebar php file but still use widgets for each individual sidebar?

    Or, would I have to hardcode html/php into each sidebar I create? I’m thinking of adding 10 different images to a sidebar depending on the post/page I’m on and I’d like to experiement to see which image would work best but I don’t want to have to create 10 different sidebar php files.

    So, can I create 1 sidebar file, such as sidebar-post.php file and use widgets with it as well as use widgets with the normal sidebar.php file?

    Thanks

  7. Scott says:

    Hi WPB,

    Thanks for the reply. What I decided to do was add the Widget Logic plugin as this seemed to be easiest.

    I’ll bookmark this page though if I want further customization.

    Thanks

  8. Alex says:

    Dear WPB,

    I followed your instructions (page, single, even index .php) pages to no avail – I called my file “ailments.php” just … because – perhaps my naming protocal is incorrect?

    Perhaps I am have misunderstood: this is exactly what I inserted:

    ID, “sidebar”, true);
    get_sidebar($sidebar);
    ?>

    Do I need to specify variants somewhere in the code you supplied and if so, where.

    I am brand-new to WP…

    Many Thanks,
    Alexandra Hattingh

  9. Olivia says:

    Hey there, thank you for the advice! This is just the sort of solution I am looking for.

    Unfortunately I followed the instructions and it appears that the custom field is being ignored and the default sidebar is being displayed. My custom sidebar works when it’s hard coded into the Page.php file, but since I want custom sidebars for more than one Page, I’d really like to work this solution to my advantage.

    I’m running WordPress 9.2, thanks for any help you can offer me.

    • Just tried it on our test blog, and it is working.

    • Jen says:

      What do you mean put it in the loop? Also when you add say sidebar and flordia. You still have to create a file called sidebar-flordia.php or does generate the fife Itself. When I tried it I got a error.

      • You have to create a separate file by yourself. Second, you have to either place this custom field code inside your loop or use global wp query tutorial that we have written on our site.

        This code is not flawed, and we implemented it for Troy on his site.

  10. Jennifer says:

    brilliant! exactly what i was looking for. and so simple! why didn’t i think of that? :) thanks…

  11. Troy says:

    I have not been able to get this to work. I have tried the following and always get the default sidebar only:

    ID, “sidebar”, true);
    get_sidebar($sidebar);
    ?>

    Since I am running WP 2.9.2 I also tried:

    ID, “sidebar”, true);
    include ($sidebar); ?>

    and received error that says: ID, “sidebar”, true); include ($sidebar); ?>

    where the sidebar should be.

    Any thoughts????

    • Remember that custom fields are only displayed inside the loop for that specific post. If your code is outside the loop, then you can either run a global wp query to get the custom field, or move this code inside your loop.

  12. kiran says:

    Awesome! exactly what i was looking for. and so simple! :)

    Thank you so much!!!

    • Jen says:

      ID, “sidebar”, true);
      get_sidebar($sidebar);
      ?>

      How is this written exactly I tried to use this and it didn’t work for me.

      • We have the code running on several client’s site and it is working fine. You have to past the php code as we suggest. Then create a separate sidebar.php file. When you are calling the custom field example: florida … then WordPress will try to look for sidebar-florida.php and display that on the specific single post.

        If you need further assistance please email us using our contact form.

  13. Surajit Basumallick says:

    this is xcelnt…..its working nice and cool documentation. thnx

  14. Jen says:

    Where to do you place the custom code that is outside the loop??

  15. Becky says:

    Thanks! This was just what I was looking for to give each page it’s own side image. Working in WP3 beta and the code was perfect as written.

  16. Anoki Casey says:

    Hey everyone… the comment example was just only showing the part that needed fixing… the whole thing is:

    ID, “sidebar”, true);
    include ($sidebar); ?>

    I got it to work, no probs.

    …a

  17. Ningbo says:

    THANK YOU very much for sharing such a great tip!
    WPBeginner rocks!

    Just wondering if the same tip could be applied to footer.
    Displaying a different footer for different pages….

    Jamy

Share Your Opinions

Tell us what you're thinking...
and if you want a pic to show with your comment, then get gravatar!

Please make sure that you have read our Comment Policy.

Due to high volume of request from our readers, we are adding this feature that allows you to stay updated with this post's comments without having to participate in the discussion even though we would love your input as always. Don't worry we hate SPAM just as much as you do, so you will never receive any SPAM messages from our site and that's our promise to you.

Subscribe without commenting

Close Bar