How to have multiple widgetized different sidebars

Its pretty much simple to widgetize multiple areas in a WordPress theme. Here in this tutorial I will demonstrate how you can have different sidebars for different pages and all of them being widgetized so that adding content to it is a piece of cake.

Lets say we want to have different sidebars as per categories.

Calling different Sidebars

We call the sidebar in our theme (sidebar.php) by a call to the function get_sidebar();

Now if we want to use a different sidebar then create another php file named like sidebar-right.php or sidebar-someother.php, we can call it by get_sidebar(‘right’); and by get_sidebar(‘someother’); respectively.

So we just need to call a function get_sidebar(‘x’); and WordPress will look for a file named sidebar-x.php for creating a sidebar.

Widgetize Sidebars

To widgetize a sidebar we just need to call the function dynamic_sidebar() with the name of that widgetized area. The proper way of doing so is by adding this piece of code

if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('Right') ) : ?>
	<p>Add widgets on Right sidebar and they will appear here</p>
	<?php endif; ?>

Similarly do that for all of your sidebars.

Now define them in functions.php file

if ( function_exists('register_sidebar') )
{
    register_sidebar(array(
		'name' => 'Left Sidebar',
        'before_widget' => '<li id="%1$s" class="widget %2$s">',
        'after_widget' => '</li>',
        'before_title' => '<div class="widget-title">',
        'after_title' => '</div>',
    ));
	register_sidebar(array(
		'name' => 'Right Sidebar',
        'before_widget' => '<li id="%1$s" class="widget %2$s">',
        'after_widget' => '</li>',
        'before_title' => '<div class="widget-title">',
        'after_title' => '</div>',
    ));
}

Now all of them will show up in Widgets option under your Appearance settings in WordPress.

Calling Sidebars based on Categories

We just need to place the sidebar conditionally by checking for conditions.

For example:

<?php
if ( is_category('1') )
get_sidebar('Left');
elseif ( is_category('2') )
get_sidebar('Right');
else
get_sidebar('someother'); ?>

You can also use the category name instead of category ID in conditions.

Similarly if you want to use the same for multiple categories then it can be done like

<?php
if ( is_category(array(1,3,4,5)) )
get_sidebar('Left');
elseif( is_category('2') )
get_sidebar('Right');
else
get_sidebar('someother'); ?>

The logic of having different sidebars under different conditions are as per ones need and creating them is pretty simple logic. If you need any help, leave here a comment and I will give you the code. 🙂

Now that’s done pretty well in WordPress. Similarly you can do that to widgetized multiple areas in your theme. Have any questions? Feel free to ask in comments. Make sure you check out the whole WordPress as CMS series.


Comments

7 responses to “How to have multiple widgetized different sidebars”

  1. When was this posted? It’s handy to have dates on your blogs so we can know whether the content still applies.

    1. The dates are now shown again with the new theme. Sorry for replying so late.

  2. Could you please specify the tutorial in steps?

    Lets say, i would like to have have different sidebars in each category.

    For example:

    Step 1: create differtent sidebars php: sidebar1.php, sidepar2.php
    Step 2: upload the files in /themes/…
    Step 3: etc. etc.

    Thanks in advance

    1. Step 3: Copy the code given in the post and paste where the sidebar is called in your regular wordpress files (index.php, single.php etc)

      That’s it!

  3. Hi,

    Thank for such a helpful post. I do have a few questions though.
    (1) I assume that is_page can replace the is_category, if I want a custom sidebar for my pages. Is that right?

    (2) Can the is_page work for subpages too? If not, how should I treat subpages so that they too have the same sidebar as their parent pages?

    (3) My current them has two sidebars: the left one is names sidebar.php, and the right is right_sidebar.php. On my home page, these are accessed with

    I would like to have variations of the right_sidebar.php for specific pages, as in right_sidebar-athletics.php. How should the code be written?

    Thank you so much for your help in advance.

    Vikky

    1. Hi Vikky,
      1 > Yes!
      2 > Get an idea from here – http://css-tricks.com/snippets/wordpress/if-page-is-parent-or-child/
      With a basic knowledge of code you can make it to work with n level deep subpages too
      3 > You can change the sidebar calls to sidebar calls embedded inside conditions as per your case.

  4. I just end up with two of the same default sidebars, one atop the other..