Make WordPress Page or Post use different sidebar

How to add a second sidebar widget for use on separate pages or posts: The following tutorial covers the process of building a second widget compatible sidebar that can be used on specific pages or posts. Using a different WordPress sidebar for pages and posts allows the user to display different sidebar content on a given post or page. In addition, the user could add the duplicate sidebar widgets plugin to add the same widget content to both sidebars.

Adding a Second Widget Sidebar to WordPress

This process assumes your using Kubrick or a theme based on Kubrick. Please backup any files before editing.

  1. Navigate to Design ->Theme Editor from your WordPress admin panel
  2. Open the functions.php file from the theme file list and find:
    if ( function_exists('register_sidebar') )
    register_sidebar(array(
    'before_widget' => '<li id="%1$s" class="widget %2$s">',
    'after_widget' => '</li>',
    'before_title' => '<h2 class="widgettitle">',
    'after_title' => '</h2>',
    ));
  3. Replace it with the following code, then update the file:
    if ( function_exists('register_sidebar') )
    register_sidebar(array('name'=>'Sidebar 1',));
    register_sidebar(array('name'=>'Sidebar 2',));

    You should now have two widget compatible sidebars that show up under Design-> Widgets.
    Next we will add a Sidebar template to use for Sidebar 2 and then call it into the page or post we wish to have display the new sidebar.

  4. Download the sidebar2.zip file and upload the sidebar2.php within to your WordPress wp-content/themes/your-theme-name/ directory
  5. Now to use Sidebar 2, simply replace the following code found in any of your template files. For example (Single Post) single.php
    <?php get_sidebar(); ?>
  6. With the following code:
    <?php include("sidebar2.php");  ?>

    Note: You can build a custom template that specifically calls in Sidebar 2 and elect to use it whenever you write a page that you wish to use this alternate Sidebar.