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.

2 Responses

Jan 06 2012 11:51

Wasn’t there a comment here before which explained that you had to name the file something specific? I don’t quite remember… sidebar-nameofsidebar.php? Anyone remember?

Jan 12 2012 18:04

Thanks for this post.

What I’m hoping to do is change my post page to a similar design as 500px.com. See this page for example: http://500px.com/photo/4274374?from=editors

The sidebar (although I’m not sure it was created as a sidebar) on the right contains details and info specific to that page, yet has social networking links below it. I’m hoping to to display details in a similar manner on the top, and then include my widgets (social networking, categories etc) below it. Any idea how to do that? Would something like the steps above work for something like that?

Thanks.

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required

WP Theme of the week

Sponsored Likebox