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.

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.
Comments
18 Responses to “Display Different Sidebar for Each Post and Page for WordPress”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.










Very nice tip and a clear explanation for setting this up. Thanks!
Rob – LexiConn
I was looking for this tip a long time, THANK YOU!!!!
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.
Really nice tipp, thanks for that! I was looking for such a simple solution for the last days and now found it here.
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); ?>
were did u put that code?
In your single.php or page.php
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
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
There is a way to do it with the category, but no way to do it with single posts.
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
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
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.
brilliant! exactly what i was looking for. and so simple! why didn’t i think of that?
thanks…
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.
I replaced the with your code exactly in the same spot. Should I place it somewhere else?