Give Posts Their Own Sidebar in WordPress

2010 January 20
by MK

One way to improve the structure of your WordPress blog is to create multiple sidebars. More specifically, make the content of the sidebar that appears on post pages different from the content that appear on the site-wide sidebar.

In this tutorial, I will show you the proper way to create a dynamic sidebar that is solely for posts.

Create a PHP File for the Sidebar

The first step is creating the actual PHP template file for the sidebar. Name the file sidebar-post.php. Put this file in the same folder where sidebar.php exists. Now copy the content of sidebar.php to the new file.

My sidebar.php looks like this:

<?php global $vigilance; ?>
  <div id="sidebar">
    <?php if ($vigilance->sideimgState() == 'hide') : else : ?>
      <?php if (is_file(STYLESHEETPATH . '/sidebar-imagebox.php')) include(STYLESHEETPATH . '/sidebar-imagebox.php'); else include(TEMPLATEPATH . '/sidebar-imagebox.php'); ?>
    <?php endif; ?>
    <?php if ($vigilance->feedState() == 'disabled') : else : ?>
      <?php if (is_file(STYLESHEETPATH . '/sidebar-feedbox.php')) include(STYLESHEETPATH . '/sidebar-feedbox.php'); else include(TEMPLATEPATH . '/sidebar-feedbox.php'); ?>
    <?php endif; ?>
    <ul>
      <?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('right_sidebar') ) : ?>
        <li class="widget widget_recent_entries">
          <h2 class="widgettitle"><?php _e('Recent Articles'); ?></h2>
          <ul>
            <?php $side_posts = get_posts('numberposts=10'); foreach($side_posts as $post) : ?>
            <li><a href= "<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
            <?php endforeach; ?>
          </ul>
        </li>
      <?php endif; ?>
    </ul>
    <?php if (is_active_sidebar('left_sidebar')) echo '<ul class="thin-sidebar spad">';?>
      <?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('left_sidebar') ) : endif; ?>
    <?php if (is_active_sidebar('left_sidebar')) echo '</ul>'; ?>
    <?php if (is_active_sidebar('right_sidebar')) echo '<ul class="thin-sidebar">'; ?>
      <?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('right_sidebar') ) : endif; ?>
    <?php if (is_active_sidebar('right_sidebar')) echo '</ul>' ;?>
  </div><!--end sidebar-->

Unless you’re using the same theme that I copied this code from, Vigilance, you’ll most certainly have different code. Look at the code you’ve copied and find what looks like this:

if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('right_sidebar') ) : ?&gt;

In your new file, sidebar-post.php, you should substitute right_sidebar (or whatever you have) with post_sidebar.

Register the Dynamic Post Sidebar

Now that you’ve created the PHP file for a widget-enabled sidebar, it is time to register this sidebar so that you can add widgets to it. You’ll have to  look into how your theme registers sidebars. In your theme’s folder, you should find a file called functions.php. In that file, look for a line that looks like this:

if ( function_exists('register_sidebar') ) register_sidebar(array(

The code in the theme that I’m using here is this:

if ( function_exists('register_sidebar') )
    register_sidebar(array(
        'name'=> __('Right Sidebar', 'vigilance'),
        'id' => 'right_sidebar',
        'before_widget' => '<li id="%1$s" class="widget %2$s">',
        'after_widget' => '</li>',
        'before_title' => '<h2 class="widgettitle">',
        'after_title' => '</h2>',
    ));

In the code above, you’d have to substitute Right Sidebar with Post Sidebar and right_sidebar with post_sidebar. Apply the equivalent changes to your own theme’s code.

Finally, Call Your New Sidebar from single.php

In most WordPress themes, a post page is displayed using the single.php template file. This file is where a sidebar gets called on. You’ll have to edit the line that calls the main sidebar to make it call the new sidebar that you’ve created above.

single.php is located at the main folder of the theme. In your single.php, look for the code get_sidebar(); and edit it to get_sidebar('post');.

That’s it! Now your post pages will display their own separate sidebar. To add widgets to this new sidebar, go to the widgets panel, find the sidebar named “Post Sidebar”, and drag widgets to it.

I hope you found this tutorial helpful. If you have any comments, questions, or suggestions, please post them below :)

Do any of these posts interest you, too?

  1. Fix Yet Another Related Posts Plugin (YARPP) Widget and Add it to the Sidebar
  2. Clean Your WordPress Sidebar to Improve Navigation and SEO!
  3. Sensible Use of “Tags” in a WordPress Blog
2 Responses leave one →
  1. March 10, 2010

    Great info. I did some VA work for you a few months ago and got interested in making my own website. I just started a few weeks ago and came by here to see if there was any info I could use. I will be trying this out.

    • MK permalink*
      March 10, 2010

      Hey Cameron! Nice to see you here again :)

      Good luck with that site; email me if you need any help with it.

Do you have a question, suggestion, or comment? Tell me!

Note: You can use basic XHTML in your comments. Your email address will never be published.

Subscribe to this comment feed via RSS