Weekend QuickTip: Two Sidebar Templates

8 Comments

Ever had a layout with two or more sidebars? The standard convention is to put both sidebars inside sidebar.php if they’re beside each other, or one in header.php and the other in the standard sidebar template. However, there’s a quick little WordPress trick that actually allows for two separate sidebar templates.

You’ll need two files: sidebar.php and sidebar-right.php. The first will contain the first sidebar on the left of the page, and the second, obviously, contains the area for the right of the page.

Now, in your template files, you can use the <?php get_sidebar(); ?> function to call the left sidebar, as usual. But now that you’ve added a second file, <?php get_sidebar('right'); ?> can be used to call the “right” sidebar file from the template. Note: the “right” file is the only file that seems to work; ‘sidebar-left’ will not.

8 thoughts on “Weekend QuickTip: Two Sidebar Templates

  1. If you would look up the get_sidebar function, there’s probably a way to hack into accepting all kinds of sidebar files. Quite possible this is doable with a plugin, however ,I’ve never created wordpress plugins.

  2. One tiny suggestion. Maybe call the secondary sidebar something like sidebar-seconday.php, or something similarly generic? If the sidebar, then, is ever switched from one side of the page to the other, this call won’t be confusing.

    This might be my desire for semantic XHTML markup bleeding over into my PHP a bit, but I’d like to think the two languages can share some principles.

    Nice tip, definitely handy when the situation calls for it.

  3. From my understanding, the name is a requirement, so if you wanted to call it something different that’s something WordPress would have to change. Good idea, though.

  4. It appears my code was stripped from the comment above.

    What I mean is why not just use php include (‘sidebar-2.php’);

  5. Hmm – I thought when I read the article in the Codex that it mentioned get_sidebar('right'); was the only one that worked. Maybe you can add anything after the hyphen – can anyone confirm that?

  6. The only gotcha with multiple sidebars is that a lot of widgets (including ones in the wordpress install) are NOT multi-widgets — which means there can only be one used once and cannot be added to additional sidebars. Not a problem in the simple example above but if you start having a lot of side bars on different pages then it’s a huge pain.

    I am going to release a free theme framework (OnePress Community) in the next week that will fix all the default widgets, add a few new cool ones and allow multiple sidebars and page customization and support phpBB user integration. The site is http://OnePressCommunity.com and should launch second week of November.

Comments are closed.