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

September 5, 2009 at 11:31 am

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

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.