Adding Multiple Sidebars To Your Theme

Written by on March 25, 2011 in Featured, WordPress Tutorials - 1 Comment

If you have done even some of the most basic WordPress theme development, then you probably understand at least the basics of how theme templates work. Today I want to go a little bit beyond the basics and show you how to work with a really excellent, useful feature of the WordPress template system.

One of the most common aspects of WP themes is the use of “sidebars”, particularly widgetized sidebars, and most good themes have multiple configuration options for your sidebars: left, right, right wide, left wide, etc. Sometimes, using CSS is powerful enough for some of the layout options, but what if you want drastically different layouts, such as horizontal, short and narrow in the footer, below the post, etc? That’s where multiple sidebar.php template files come in handy.

By default, a theme’s sidebar is defined as what ever is in the file sidebar.php, and the contents of that file are then displayed with the function call

1
get_sidebar();

Well, let’s presume that we want to have three distinctly different sidebar layouts: one tall and narrow on the right (we’ll call this the default), one just below the post area in the main content section, and one in the footer, and all of them are widgetized. First what you need to do is create three files:

  • sidebar.php
  • sidebar-posts.php
  • sidebar-footer.php

Next, you can use the same function I mentioned a moment ago to display the contents of the sidebars:

1
get_sidebar();

however, now, in order to pull in either the posts or the footer sidebar, we just need to add one parameter to the function:

1
2
3
get_sidebar('posts');
// or
get_sidebar('footer');

Whatever the name is you placed after the “-” in the sidebar file name, is the parameter you will pass to the function.

Now you can easily have distinct “sidebars”, and as many as you want, that each have their own layouts, styles, widgets, etc. But remember, in order for your sidebar to be able to accept widgets, you have to enable it. If you don’t know how to do that, check out the codex entry on register_sidebar.

About

Pippin Williamson is an expert WordPress dev with 4+ years of experience. You may follow him on Twitter @pippinspages and @pippinsplugins and see his free WP plugins, themes, and tutorials he has to offer at Pippin's Pages.com and Pippin's Plugins.com

Post comment as twitter logo facebook logo
Sort: Newest | Oldest

I have looked at MANY wordpress sidebar posts. This one a) had the most recent datestamp at Mar. 25th, 2011, and it also had b) SUPER straight-forward instructions, leaving out how to REGISTER/enable sidebars and instead linking to the wordpress official codex for that.

Bravo!