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!
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.
34 Responses
nice hack, stumbled!
Thanks, glad you enjoyed it
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”
Till that time that hack will come in handy. Thanks, JBJ.
Great work. Thanks
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…
OMG. I love it thank you! <3
Thanks jean. It’s simple way like a conditional tags
Thanks, That is one cool i’ve tried
Ohhh… Cool…Thanks for the Hack
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.
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
You never fail to impress!
thanks for this hack!
Very helpful!
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.
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!!!
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
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
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.
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!
Hello friends works fine, but if you want a post has no sidebar?
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.
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.
Trackbacks: