Post Pic

WordPress hack: Choose the sidebar to use, post by post

Who said a blog always have to display the same boring sidebar on each page? Just create a few sidebar files, upload it to your theme directory. This cool code will do the rest!

Simply open single.php* and find the call to the get_sidebar() function:

<?php get_sidebar(); ?>

Replace it by:

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

Now when you'll write a post, create a custom field name sidebar. As a value, give it the name of the sidebar you want to include. For example, if the value given is right, WordPress will automatically inlcude right.php as a sidebar.

*The same can be done on page.php.

36 Responses

Sep 05 2009 12:28

nice hack, stumbled!

Sep 05 2009 12:55

Thanks, glad you enjoyed it :)

Sep 05 2009 13:42

That is one cool hack! I’m looking forward when future premium themes will have features included like “chosse sidebar”, “choose page template”, “choose everythingelse” :D
Till that time that hack will come in handy. Thanks, JBJ.

Sep 05 2009 14:20

Great work. Thanks

Sep 05 2009 15:43

I already do this, but thanks any way because it’s a very useful tip for everyone. Talking about the sidebar, we can also add different footers or headers… Of course if that makes any sense…

Sep 06 2009 03:06

OMG. I love it thank you! <3

Sep 06 2009 10:14

Thanks jean. It’s simple way like a conditional tags

Sep 06 2009 10:16

Thanks, That is one cool i’ve tried :)

Sep 06 2009 13:17

Ohhh… Cool…Thanks for the Hack

Sep 14 2009 17:44

Great simple tut there, I was actually thinking about this today whilst planning a new project.

I am finding that custom fields are v.powerfull.

:) will be working with this tonight

Sep 15 2009 12:56

Hi, I’ve got a slight problem. I dont have a single.php file in my theme but I tried to add it on the page.php and all i can find is this code;

Should I replace it anyway?

Thanks,

N

Sep 15 2009 18:06

You never fail to impress!
thanks for this hack!
Very helpful!

Sep 24 2009 05:27

Dynamic sidebar is a little trick I like, but sometimes it can be a mess. You need create various sidebars and register a lot of widgets areas.
I did something very similar with pages and categories.

Sep 24 2009 18:44

Hello! This is exactly what I was looking for! Unfortunately, I can’t make it work ;(. It it working on wordpress 2.8.4?

Here is what I have done:

1) Replaced get_sidebar() function for the one you gave us in single.php.

2) Created a new sidebar.php. My new one is call sidebar_nwt.php. To create it, I simply made a copy of sidebar.php and made some minor change to text. At the top of sidebar.php, there was a so I changed it for in sidebar_nwt.php.

3) I created a new post and created a custom field name sidebar. The value is sidebar_nwt

Can you tell me what I’ve done wrong?

Thank you very much!!!

Sep 28 2009 14:36

Hello again! I finally managed to make it work. Here is what I have done:

Changed the value of my custom field from sidebar to just “nwt”.

Then renamed sidebar_nwt.php to sidebar-nwt.php

Oct 16 2009 04:46

I like this hack, thanks!

But, what I’m wondering is, if I create a new sidebar-whatever.php can I still use my widgets for each sidebar? Does anyone know if this is possible?

I can hardcode my html/php into each individual sidebar php file but I’d like to be able to assign a sidebar but use widgets, for example to swap out different images, etc, over time.

To hardcode the image/html into each sidebar then that would mean, I assume, that each sidebar php file would have to have a different image and if you have 10+ images, that’s a lot of files.

Can you do the hack above and use widgets still?

Thanks

Oct 16 2009 11:45

What I’ve currently done is not strip out the sidebars completely. I still use the homepage sidebars which is vital for making navigation easy for my users.

But the post pages now use full-width page.php etc, so I dont have the function of using the sidebars, what I do have is called the ‘post options’ so I can add anything anywhere on the post even the sidebar!

I’ve got a cool programmer helping me do this for a good price if you want to know more.

Oct 16 2009 15:44

Hi Visitlocal, not sure if your comment is pertaining to me or not but it’s always good to know a good programmer :)

What I ended up doing was using the Widget Logic plugin which does what I want without me hacking the php code. I basically wanted to add an ad in the sidebar on a particular page or post and this plugin does just that by calling conditional tags and using a Text widget.

Works great so far!

Oct 22 2009 18:05

Hello friends works fine, but if you want a post has no sidebar?

Dec 18 2009 13:45

Or you could use either Widget Logic or Widget Context to specify which widgets appear on which sections of the site, be it page, post, category or archive listing.

Dec 29 2009 06:31

What a cool hack, never thought of this before, but now thats the code and concept been shown, the possibility are endless. This site makes me admire WP even more. And this site off course.

Feb 19 2010 07:13

how would I go about setting this up so it would default to a sidebar if no sidebar is chosen?

Mar 10 2010 05:53

Can you please tell me how to display list of items only in the single.php page. I first thought of creating two sidebars,but somehow felt that using if condition in sidebar php to display only in the single will be efficient. What do you say?

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required