How to have multiple widgetized different sidebars
November 11th, 2009 | Tagged as: CMS ¤ WordPress | Download as PDFIts 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-two.php or sidebar-three.php, we can call it by get_sidebar(‘two’); and by get_sidebar(‘three’); 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.
For example: <?php dynamic_sidebar(‘Sidebar 2′); ?> in sidebar-two.php
Similarly do that for all of your sidebars.
Now define them in functions.php file
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>',
));
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>',
));
}
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();
elseif ( is_category('2') )
get_sidebar('two');
else
get_sidebar('three'); ?>
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();
elseif( is_category('2') )
get_sidebar('two');
else
get_sidebar('three'); ?>
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.




Howdy! My name is Ashish K Saini aka Ashfame.

When was this posted? It’s handy to have dates on your blogs so we can know whether the content still applies.
The dates are now shown again with the new theme. Sorry for replying so late.
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
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!