How To Create Separate Sidebar Widgets Per Page in WordPress

Widgets area in wordpress admin

Most WordPress themes come with the “Widgets” function in the dashboard (Appearance –>  Widgets), that allows you to control the sidebars for all your site pages.  However, for the most part, these are global settings, in other words, for every page in your site, it will apply the widgets accordingly.

So how do you set different widgets to show up on different pages of the site?  Here is one work flow that worked for me:

  1. Create different templates for each page layout you want.  For example, you might have a 2 column layout page and a 3 column layout page.  Each layout would have different content you want to appear in their sidebars.  You would then create two templates:  2-column-layout and 3-column -layout.
  2. When you create the template file, add the following code where you would like to see be controlled by widgets.  For example, if you want a widget controlled right side bar for your two column template, then add the following code:
    <?php if (!function_exists('dynamic_sidebar') ||
    !dynamic_sidebar('two-column-right')) { ?>
  3. Now you must tell WordPress Widgets to recognize your new sidebar entry.  Open Functions.php, and add the following code:
    if (function_exists('register_sidebar')) {
           register_sidebar(array(
                   'name' => 'two-column-right',
                   'id' => 'two-column-right',
                   'before_widget' => '<div class="widget">',
                   'after_widget' => "</div>",
                   'before_title' => "<h4>",
                   'after_title' => "</h4>",
                   ));
    }
  4. Create the pages in your site, and make sure to choose the correct template.
  5. Now go the wordpress admin section, click on Appearance –> Widgets, and you should see your new side bar entry “two-column-right”.

Sources: http://automattic.com/code/widgets/themes/

One thought on “How To Create Separate Sidebar Widgets Per Page in WordPress

Leave a Reply