How to add multiple widget sidebars to your WordPress blog
Our first ever guest post is by Anthony Hortin of Maddison Designs.
You can follow Anthony on Twitter @maddisondesigns
I’ve found that the term “sidebar” within WordPress can have multiple meanings. Within your index.php file you will most likely find a call to the php function get_sidebar(). This is referring to the sidebar column, sometimes called the right-hand (or left-hand) navigation column. A sidebar also refers to the location where you add all your Widgets. The Widgets control the content that appears within the sidebar (column) and will usually display lists such as your Recent Posts, Categories, Links or dozens of other items.
Since WordPress is so configurable, it’s possible to have multiple sidebar columns defined such as when you have multiple page templates set up. For example, you may have a specific page template defined for your sites static homepage. As well as defining the body content for that page, you can also define a separate sidebar in case you would like it to appear differently from the rest of the site.
It’s also possible to register multiple sidebars that contain all the widgets. This latter sidebar is what we’ll be discussing here. A typical WordPress theme may only have one sidebar configured although it’s not uncommon to have 2 or 3 or even more. Even though it’s called a “sidebar” it doesn’t need to specifically reside in the sidebar of your website. You can add a “sidebar” to anywhere in your site you want to display your widgets. A common place to include them is the site footer.
Defining your new sidebar
Adding additional sidebars to your site is relatively easy. Within your functions.php file you will most likely have something similar to the code below, for your existing sidebar.
<?php
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>’
));
}
?>
To add another sidebar you simply need to register another instance. If you are registering more than one, then you will also need to name them. Change your functions.php file to look like the following. Take care to add in the extra line that specifies the name of your sidebar. Once these are functions are defined, you will notice the extra sidebar appear in the WordPress Dashboard under the Appearance > Widgets option. It’s here that you can drag and drop all your widgets into your various sidebars.
<?php
if ( function_exists(‘register_sidebar’) ) {
register_sidebar(array(
‘name’ => ’sidebar 1′,
‘before_widget’ => ‘<div id=”%1$s” class=”widget %2$s”>’,
‘after_widget’ => ‘</div>’,
‘before_title’ => ‘<h2>’,
‘after_title’ => ‘</h2>’
));
register_sidebar(array(
‘name’ => ‘footer sidebar 1′,
‘before_widget’ => ‘<div id=”%1$s” class=”widget %2$s”>’,
‘after_widget’ => ‘</div>’,
‘before_title’ => ‘<h2>’,
‘after_title’ => ‘</h2>’
));
}
?>
You can find more details on the various parameters of the above call on the WordPress Codex.
Adding your new sidebar to your template
Within your sidebar.php file, change the call to your existing sidebar to include its name that you defined within the functions.php file earlier.
<?php if ( !function_exists(‘dynamic_sidebar’) || !dynamic_sidebar(’sidebar 1′) ) : ?>
<h2>Articles by month</h2>
<ul>
<?php wp_get_archives(‘title_li=&type=monthly’); ?>
</ul>
<h2>Categories</h2>
<ul>
<?php wp_list_categories(’show_count=0&title_li=’); ?>
</ul>
<?php endif; ?>
To add your new sidebar, you can either copy the above code or you can simply copy the following lines. Add these lines to wherever you’d like your new widgets to appear. In this example you can see from the name that I’m placing mine in the footer of my website. As before, don’t forget to specify the correct sidebar name. In the above code, the html that appears between the php statements is what will appear when there are no widgets added to your sidebar. This ‘default’ code can obviously be modified to suit your theme. In the following code, since there is no extra html, nothing will be displayed unless a widget has been added into the sidebar within your WordPress dashboard.
<?php if ( !function_exists(‘dynamic_sidebar’) || !dynamic_sidebar(‘footer sidebar 1′) ) : ?>
<?php endif; ?>
You will now find that your site has two widgetized sidebars. As mentioned earlier, there’s no reason why you couldn’t include further ones. It’s simply a matter of registering the new sidebar and then including the php to display it, in the relevant part of your site template.
This entry was posted on Monday, March 1st, 2010 at 2:46 pm and is filed under Misc. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.




Fiona Fell - The Profit Maximising Web Geek March 1st, 2010 at 3:10 pm
Thanks for the clarification. Some times my brain insists on getting the two types of ‘Sidebar’ confused.
Have now filed them in my brain as ‘Sidebar’ and ‘Sidebar Areas’. Hoping my brain keeps this a bit longer so that I can design all sorts of wonderful menus and footers.