How to use widgets with more than one sidebar on your WordPress blog

Filed as Features, Guides on May 24, 2007 2:39 am

In continuation of my last post, Enabling sidebar widgets for your WordPress theme, I am now going to show you how you can use these newfound widgets with more than one sidebar on your WordPress blog.

Most blogs have only one sidebar, but some, such as Blog Herald, have two (or more!) After reading my last post, you learned how you can use widgets on your blog’s theme, so now, I will show you how you can use widgets on two or more sidebars. This post assumes that you’ve either read my last post, or you already know how to widgetize a theme but would like to know how to widgetize more than one sidebar.

I’m also going to show you how you can customize your sidebars by choosing how you want each widget to be formatted on a per-sidebar basis, and I’ll also show you how you can name your sidebars to more easily identify each one.

This tutorial will focus on using widgets on two sidebars, but the steps can be easily reproduced to adapt to more than two sidebars.

First of all, if you don’t have a file named functions.php in your WordPress theme’s folder, then create one. The contents should be similar to the following:

<?php

if (function_exists('register_sidebar'))
{
register_sidebar(array(
'before_widget' => '',
'after_widget' => '',
'before_title' => '',
'after_title' => '',
'name' => 'Left sidebar'
));

register_sidebar(array(
'before_widget' => '',
'after_widget' => '',
'before_title' => '',
'after_title' => '',
'name' => 'Right sidebar'
));
}

?>

Be sure to change the value of ‘name’ to whatever you want; right now, the first sidebar’s name is set to ‘Left sidebar’ and the second sidebar’s name is set to ‘Right sidebar’. These are pretty logical names, but you may want to rename them to something that is more meaningful for yourself. You can also copy and paste each ‘register_sidebar’ function several times to create more sidebars; be sure to make the ‘name’ value for each sidebar unique from the others, or conflicts will occur!

The other arguments that you see in the code above are pretty self-explanatory; you can place HTML code in the 4 other arguments besides ‘name’. The HTML that you place in ‘before_widget’ goes before the widget; ‘after_widget’ goes after the widget; ‘before_title’ goes before the widget’s title within the widget; and ‘after_title’ goes after the widget’s title.

The first four are documented by Automattic already, but strangely enough, ‘name’ is not; I would consider this one the most important one, too.

Once you have created your functions.php file, it’s time we widgetize your theme. Now, this time it’s a bit different because we’ll be adding more than one widgetized sidebar to your theme. I’m going to assume that you already have at least two sidebars, but that they aren’t widgetized yet.

Just like in my previous tutorial, you’ll have to open up your sidebar.php file in your /wp-content/themes/<style name> directory.

Secondly, look for the very first <ul (yes, it’s purposely missing the closing >). This may look like <ul>, or <ul id=”sidebar”>, or something similar. Immediately after this line of code, place

<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('Left sidebar') ) : ?>

Replace ‘Left sidebar’ with your actual sidebar’s name that you’d like to go here. It should end up looking something like this:

<ul id="sidebar"><?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('Left sidebar') ) : ?>

Then, find </ul> and immediately before that, place

<?php endif; ?>

Do this for every sidebar that you have; so you’d have another line like so:

<ul id="sidebar"><?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('Right sidebar') ) : ?>

And we’re all done! Easy, isn’t it?

Gary King is a professional freelance web developer, primarily using Ruby on Rails and PHP to create cool new websites. When he’s not trying to take over the world one blog at a time, you can find him mulling over his thoughts at King Gary.

Tags:

This post was written by

You can visit the for a short bio, more posts, and other information about the author.

Submissions & Subscriptions

Submit the post to Reddit, StumbleUpon, Digg or Del.icio.us.

Did you like it? Then subscribe to our RSS feed!



  1. How to use widgets with more than one sidebar on your WordPress blog | King GaryMay 24, 2007 at 2:42 am
  2. By Joatp Blog posted on May 24, 2007 at 9:23 am
    Want an avatar? Get a gravatar! • You can link to this comment

    Very nice post. So far I always use free theme using widgets like TerraFirma, I am planning to create my own theme and this article will help me. Thanks.

  3. SOB Business Cafe 05-25-07 - Liz Strauss at Successful Blog - Thinking, writing, business ideas . . . You’re only a stranger once.May 25, 2007 at 2:00 pm
  4. 收拾了一番blog的外观 | broom's blogJune 8, 2007 at 1:06 am
  5. How to rename widgetized sidebars in Wordpress | Wordpress GarageNovember 20, 2008 at 6:19 am
  6. Disha Hosting » How to rename widgetized sidebars in WordpressJuly 31, 2009 at 5:56 pm
  7. ceo`s blog » Blog Archive » How to rename widgetized sidebars in WordpressAugust 14, 2009 at 3:00 pm
  8. How to rename widgetized sidebars in Wordpress « 5edma WordPressOctober 3, 2009 at 3:01 am