Dukeo » WordPress » Post Custom Sidebars: How To Use a Different One for Each Post And Page

Post Custom Sidebars: How To Use a Different One for Each Post And Page

Steven 9 responses WordPress

You probably didn’t know it, but you are not forced to always display the same sidebar on all your WordPress posts and pages. There is an extremely simple way to display custom sidebars on certain posts by implementing the following code.

First of all, you need to create your custom sidebar files and upload them in your own theme folder.

Then, you need to open your single.php and page.php files in your WordPress theme folder and search for the code:

<?php get_sidebar(); ?>

Simply replace this line by the following code:

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

From this point, when you’ll publish a new post or a new page, create a custom field named sidebar in the post.

As an example, if you give it the value sidebar-3213, WordPress will automatically use the file named sidebar-3213.php as a sidebar.

If you enjoyed this article,
Get email updates

Join for Free »


  1. Liz

    Whoa! Crazy-cool! I like. :)

  2. Shad

    Uh-oh. I think this one’s going over my head a little bit. Can anyone tell me exactly what this will do to my page? Getting complicated…

    1. Anon

      Ok. Take a deep breath. Remember: It’s going to be ok.

  3. Michelle

    The more of these you post, the more fabulous everyone’s pages are going to look! Keep ’em coming!

  4. Chris

    Wow, you’re absolutely right, I did not know that! @Shad, dude just try it. This guy knows what he’s talking about. It’s bound to be an improvement, and ask yourself this: Has he ever steered you wrong? :)

  5. Sarah

    I love these! This is super cool. I would never have thought to figure this out for myself haha

    1. John

      Me neither! We’re lucky that this guy knows so much about it and we can just copy and paste…

  6. Affmaster

    Interesting, I’ll have to try this. Thanks for letting us know!

  7. Chris

    This is really great, I would really appreciate some real life example code of how to further implement it,

    For example.
    I wish to display a photo in the side bar, a different photo per post.. i would like to simply paste the link to the image in to the custom field and have it display in the sidebar on that particular post or page..

    And to further make this amazing, I would love some option, like another custom field, in order to program the width and height, even though I personally intend cropping the photos precisely to the right size. It would still be an awesome bit of code