WordPress – Different Sidebars In Different Places

It might, at some point in a WordPress powered blog, become necessary to a show some widgets/stuff in some places (the main page, archives, etc.) while show a different set of widgets/stuff in some other places (static pages – about me, product reviews, etc.). To keep track of things and may be understand the process better, let us suppose that only those pages written in Product Reviews Template will use a different sidebar (Sidebar #2) while rest of the blog uses a common sidebar (Sidebar #1). Thanks to my dear friend, Amy – if not for her, I probably would have never learned about this hack.


functions.php

Within the currently used WordPress theme, one can find functions.php. Backup this file before making the below given editions. At the very beginning of the file, one should see:

1
2
3
4
5
6
7
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>',
    ));

Change this to look like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
if ( function_exists('register_sidebar') )
    register_sidebar(array(
        'name' => 'Sidebar 1',
        'before_widget' => '<li id="%1$s" class="widget %2$s">',
        'after_widget' => '</li>',
        'before_title' => '<h2 class="widgettitle">',
        'after_title' => '</h2>',
    ));
 
    register_sidebar(array(
        'name' => 'Sidebar 2',
        'before_widget' => '<li id="%1$s" class="widget %2$s">',
        'after_widget' => '</li>',
        'before_title' => '<h2 class="widgettitle">',
        'after_title' => '</h2>',
    ));

This edit defines an additional sidebar. Save and close the file.


sidebar2.php

Within the currently used WordPress theme, one can find sidebar.php. Make a copy of this file and save it as sidebar2.php. One can edit the contents of sidebar2.php to place static content or use the Admin Dashboard to pick a certain set/ordering of widgets. For this particular case, I decided to leave it unchanged.


Product Reviews Template

Within the currently used WordPress theme, one needs to create a file called product_reviews.php, with the following contents:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?php
/*
Template Name: My Product Reviews
*/
?>
 
<?php get_header(); ?>
 
<div id="content" class="narrowcolumn">
 
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
 
<div class="post" id="post-<?php the_ID(); ?>">
<h2><?php the_title(); ?></h2>
 
<div class="entrytext">
<?php the_content(); ?>
<?php link_pages('<p><strong>Pages:</strong> ', '</p>', 'number'); ?>
</div>
 
</div>
 
<?php comments_template(); ?>
<?php endwhile; endif; ?>
<?php edit_post_link('Edit this entry.', '<p>', '</p>'); ?>
 
</div>
 
<?php include('sidebar2.php'); ?>
<?php get_footer(); ?>


Writing Product Reviews

When writing product reviews, all that one needs to do – apart from writing the review itself – is to make sure to pick My Product Reviews as the Page Template. Such pages will have a different looking sidebar, owing to the above changes.


Screenshots

Selecting Widgets for Sidebar 1

Selecting Widgets for Sidebar #1


Selecting Widgets for Sidebar 2

Selecting Widgets for Sidebar #2


Sidebar 1

Sidebar #1


Sidebar 2

Sidebar #2