Beginner's Guide for WordPress - Start your WordPress Blog in minutes.
Choosing the Best
WordPress Hosting
How to Easily
Install WordPress
WordPress Plugins
View all Guides

Display Different Sidebar for Each Post and Page for WordPress

Last updated on by
Special WordPress Hosting offer for WPBeginner Readers
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);

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.

Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi. Page maintained by Syed Balkhi.

WPBeginner's Video Icon
Our HD-Quality tutorial videos for WordPress Beginners will teach you how to use WordPress to create and manage your own website in about an hour. Get started now »


  1. Barry Hunt says:

    As with many tutorials online half the information is missing

    How do you create the custom side bar file

  2. goin says:

    Hi i Search net and get this post and have one question –
    How To Show Post Thumbnail In his Sidebar – user go to post and show Thumbnail of post in just sidebar – any idea which code i must used and custom sidebar.php?

  3. Cliff says:

    What if I only want the recent post to show in the side bar for the page I’m on. For instance…

    Chris Rock page –> sidebar only recent post for Chris Rock

    Also, this has to be dynamic…so every page works the same way with the sidebar…

    Web page –> recent posts in sidebar for that page


  4. Terry says:

    A simpler solution would be to just download the SiteOrigin widget this does everything for you and creates beautiful sidebars on any page you have created. It’s so simple to use – you’ll never go back to alter code again.

    • Sasa says:

      Terry can you please specify what widget and how to achieve this. I need to place a booking calendar using a shortcode in to a sidebar, but I need Joe’s booking calendar to be visible when viewing Joe’s listing, and Sandy’s booking to be visible when looking at Sandy’s listing, and so on. And to be hones I don’t have a clue how to do it.

  5. Florian says:

    Hello great article, thanks a lot. Does the same also work for footer.php? I followed your instructions and used footer instead of sidebar but my footer does not change…

    Thank you for your answer.

  6. rashid says:

    i want to add sidebar only on my home page,
    but i am no Sucess,
    it display home as well as other pages ,
    for example:
    1.add calander on home page but it automatically display other page also,
    2 archieve

  7. Adam says:

    Many Thanks! Great resource for newbies :) !
    Just wondering if the code for creating a custom header field would look something like this…

    ID, “header”, true);

    /**** CUSTOM HTML CODE HERE *****/


  8. Ernesto Dominguez says:

    Hi, I registered my custom sidebar in the functions file and added the code mentioned above to the single.php but the sidebar with widgets is not showing up. I’m using twenty twelve child theme and WP 3.9 I created a a file named directory_sidebar.php with the sidebar content and put it on the child theme directory. I also added the custom field with “sidebar” and the file name as a value. I wonder where I messed up?

  9. Bert says:

    Nice work! Will it work with the Twenty Fourteen theme as well? Actually I will hide a sidebar on pastpage with this solution..


  10. Mark Hannon says:

    I like your solution. It’s simpler than the solution I came up with:

    ID, ‘Sidebar Content’, true);

    if($custom_sidebar != ”) {
    } else{
    dynamic_sidebar(‘Default Sidebar’);

  11. kyleey says:

    Hi WP Beginner,

    This is in regards to my website – as you can see, I have only a single sidebar on the right. However, I want to add a second one (on the left). Do you have any tutorials for that? I’ve been looking around for a while, but every time I try, I end up breaking the theme completely. It’s beginning to get irritating.

    Please get back to me!


  12. Koja says:

    Nice article! Really helpful!

  13. Colin Eberly says:

    This works PERFECT!

    One thing I wanted to note that wasn’t in your write-up, is that once you create your sidebar-name.php file you will need to register your custom sidebar in the functions.php file in order to fill it through the WordPress Widgets admin area.

    Thank you so much for the article!

    • Editorial Staff says:

      Hey Colin,

      Yes that is correct. If you are looking to use widget areas in your custom sidebar files, then you have to register a custom sidebar/widget area in your functions.php file.

  14. Thomas says:


    This works great, thanks! How would I go when I’d like to use a checkbox instead of ‘nameofmysidebar’ in the meta value field?

    I know how to add the checkbox in my meta box but not how to adjust your code…

    Many thanks in advance for your help!!



  15. ebeta says:

    I’m still having problems with this…

    I tried the original code, but it went back to the old sidebar

    then I tried the updated code below, and now get NO sidebar

  16. MelissaTaylor says:

    If you’re using WordPress there is a plugin for this… “Custom Sidebars”. It’s really easy and free. :)

  17. boggs.chris says:

    Awesome solution, worked flawlessly the first time I tried it. I have been scouting various custom sidebar solutions and this one works great for me since I only need to have 2 different sidebars and not complex conditional code using only one sidebar file. This is easier for me to come back and make changes at a glance, say a year later because the sidebar files are individual files and logically named. For anyone else’s benefit, I am using this code in a child theme of twenty eleven and it works fine. Thanks Editorial Staff!

  18. Matt says:

    i tried to use information from another site in order to create a seperate sidebar for another page and my whole site is down now! when i try to access any part of the site i get this message “Parse error: syntax error, unexpected ‘}’ in /home/content/n/y/r/nyr2008/html/wp-content/themes/easel/functions.php on line 100”

    please help me! is there anything i can do to access it so i can save my site?!

  19. Danny says:

    This is exactly what I’ve been looking for and works perfectly, thank you.

    So many WordPress tutorials I don’t know where to begin!

    Home page bookmarked for future use. Thanks again.

  20. Jen says:

    Is there an easy way to add a sidebar so that you can change the background on a page or post. In other words a the background color will change per page on the sidebar.

  21. Mike says:

    Or… you could install a plugin like this one that I just wrote and plublished. The Graceful Sidebar Plugin creates a widget that enables you to use custom fields in a post or page that display in the sidebar. You can use it to display custom html or a simple message. The custom fields it uses are graceful_title and graceful_content.


    • Mike says:

      The Graceful Sidebar Plugin has been updated to replace dependence upon custom fields with customized input boxes so you don’t have to remember the name of the fields in order to create custom sidebars widgets for each of your posts or pages… you can find more information at

  22. Ningbo says:

    THANK YOU for your help and yes I can testify it works as I have it working perfect on my site for a while now :-)

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


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


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

  26. Jen says:

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

  27. Surajit Basumallick says:

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

  28. kiran says:

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

    Thank you so much!!!

    • Jen says:

      ID, “sidebar”, true);

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

      • Editorial Staff says:

        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.

  29. 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);

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

    • Editorial Staff says:

      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.

  30. Jennifer says:

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

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

    • Editorial Staff says:

      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.

      • Editorial Staff says:

        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.

  32. 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);

    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

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


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


  35. 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); ?>

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

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

  38. Alex says:

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

  39. Rob Mangiafico says:

    Very nice tip and a clear explanation for setting this up. Thanks!

    Rob – LexiConn

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.