Multiple WordPress Sidebars with get_sidebar()

Summary:
In this article I show you how to use the get_sidebar() function to call multiple sidebars (as many as you want) in your WordPress theme.
buzz_button

If you’ve been theming WordPress for a bit you may have come across the need for multiple sidebars, but you may have also realized that the default method of including a sidebar, using the get_sidebar() function, only allows you to include a single sidebar named sidebar.php (without using parameters). There’s one obvious way to include a sidebar and that’s by using a standard php include like so:

<?php include (TEMPLATEPATH . '/right-sidebar.php'; ?>

This method works, but in the interest of keeping our code readable and simplifying the inclusion of multiple sidebars, there’s an easier way to do this and it’s incredibly simple.

Including sidebar-left.php and sidebar-right.php using get_sidebar

Let’s say we’ve got two sidebars, right and left, and we’ll name the files sidebar-right.php and sidebar-left.php respectively. To include those files, all we have to do is use the get_sidebar function but add parameters to each function call to tell WordPress which sidebar we want included where.

To include the file “sidebar-right.php” we’ll use:

<?php get_sidebar('right'); ?>

And to include our other sidebar, “sidebar-left.php” we’ll use:

<?php get_sidebar('left'); ?>

Likewise, if we want to include a third sidebar, sidebar-right-2.php for example, we’ll use:

<?php get_sidebar('right-2'); ?>

Syntax of get_sidebar()

It’s that simple. The key to remember here is that, as commentator Rakeshkumar Mehta points out, the get_sidebar function prepends “sidebar- to the argument you feed it, so all your sidebar files should start with “sidebar-”. For example, to include a file named sidebar-newsidebar.php, use get_sidebar(‘newsidebar’), or to include a file named sidebar-my_right_sidebar.php, use get_sidebar(‘my_right_sidebar’).

 
Bookmark and Share

11 Comments so far ↓

  1. Please note that the get_sidebar function actually prepends “sidebar-” to everything in parentheses.

    For example, if you have a file named sidebar-right.php, you call it with get_sidebar(‘right’);

  2. Daniel F. says:

    @Rakesh: I had that problem in a bunch of the themes I was using. I got it fixed and updated all the wordpress themes on http://www.allwoothemes.com, I made it now that there is a widget pair of which one sidebar is the searchfield and the other is the results. You can see the result on the themes hosted at my site.

  3. Alex says:

    I had the mentioned problem as well. I managed to have it fixed, although it didnt look too well in IE, while no problems with Firefox.

  4. Caltz says:

    Very nice! Helped me alot! Thanks!

  5. Sam says:

    Mate… let me tell you one thing. You are a freaken angel! I was looking for this for ages and it’s now the second time that I read an article on a wordpress thing on your site and the way you describe things is just great. Keep going mate your site rocks! cheers and big hug

  6. Sam says:

    Actually… just something to consider. As far as I can see there is no plug in (or no considerable good on) doing what I was looking for. Let say we are using multiple template files for wordpress pages and we want to use different sidebars per template. It would be great to have a plugin which creates some sort of dropdown under pages/post to choose a sidebar template, which you create by duplicating/creating a “sidebar-2.php”. The advantage here would be that you can dump some hardcoded php/html in the page as well having an option to drag some widgets on that sidebar – not one or the other.

    However, didn’t find anything like it. Just some brainstorming here.

    cheers

  7. Cindi says:

    Thank you SO MUCH for the simple explanation of how it knows to use sidebar-xxx.php. My sidebar did not show, but based on examples, I thought I had everything right. Just needed to remove sidebar-.


Leave a Reply

Email is required but will not be published. All comments are moderated and no-followed. Please do not use keywords or domains as names and do not advertise.