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
Follow WPBeginner on YouTube
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 lovers 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 »
  • Bert

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


    • Bert

      …pastpage… I meant ‘post pages’..

  • Mark Hannon

    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’);

  • kyleey

    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!


    • WPBeginner Support

      It looks like your WordPress theme does not support two sidebars on both sides.

  • Koja

    Nice article! Really helpful!

  • Colin Eberly

    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

      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.

  • Thomas


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



  • ebeta

    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

  • MelissaTaylor

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

  • boggs.chris

    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!

  • Matt

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

    • Editorial Staff

      Remove the code that you added from another site.

  • Danny

    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.

  • Jen

    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.

    • Editorial Staff

      You can simply use this technique and do it via CSS.

      • Jen

        But wouldnt you have create a new sidebar for each background?

  • Mike

    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

      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

      • Editorial Staff

        Thanks for sharing the plugin Mike. Would you like to do a writeup about your plugin?

  • Ningbo

    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 :-)

  • Ningbo

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


    • Editorial Staff

      Yes the same trick can be applied for footer and header.

  • Anoki Casey

    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.


  • Becky

    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.

  • Jen

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

    • Editorial Staff

      Please contact us through our contact form for support.

  • Surajit Basumallick

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

  • kiran

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

    Thank you so much!!!

    • Jen

      ID, “sidebar”, true);

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

      • Editorial Staff

        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.

  • Troy

    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

      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.

      • Troy

        I replaced the with your code exactly in the same spot. Should I place it somewhere else?

  • Jennifer

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

  • Olivia

    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

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

    • Jen

      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

        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.

  • Alex

    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

  • Scott

    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.


  • Scott

    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?


    • Editorial Staff

      There is a way to do it with the category, but no way to do it with single posts.

  • Black Osama

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

    • john wood

      were did u put that code?

      • Editorial Staff

        In your single.php or page.php

    • Sam H

      for what it’s worth, on wpmu 2.8.6 the code that works is the one mentioned in the article, rather than this comment :)

  • Ben

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

  • Max Weaver

    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.

  • Alex

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

  • Rob Mangiafico

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

    Rob – LexiConn