Multiple Sidebars the Right Way

Multiple Sidebars the Right Way

Posted on 18. Nov, 2009 by Alex Denning in Quick Tips

Multiple sidebars are becoming increasingly popular with specific sidebars being displayed only on specific pages or posts. How do they do it? Originally it’d be an if (ie if is homepage) and then include a PHP file, but WordPress has a better way. A built in better way. In this post we’ll learn how to add multiple sidebars the right way.

The old, rubbishy way

Back in the day, you’d have something like this:

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

Which isn’t too pretty. Because WordPress has a better way of doing things. Which is using the get_sidebar function that is normally used to get the sidebar.

The new, brilliant way

It’s really as simple as renaming your new fancy sidebar sidebar-something.php and then you can use the get_sidebar function like so:

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

And that’s all there is to it. I would write more, but that’s essentially it. So I won’t. Enjoy your new found knowledge!

No related posts.

Tags:

Follow on Twitter! Subscribe!
Alex's Gravatar

Alex Denning is the founder of WPShout. A WordPress developer from London, Alex co-founded WPShift at the start of 2010 where he sells awesome WordPress themes.

You can find Alex on Twitter and at AlexDenning.com.

9 Responses to “Multiple Sidebars the Right Way”

  1. Sara

    18. Nov, 2009

    Nice to know about, thanks!

    Reply to this comment
  2. Cracks

    16. Dec, 2009

    So why exactly is the get_sidebar version is better?

    Is it just a “pretty” factor, or is it less load heavy? Please explain.

    Reply to this comment
  3. Rebecca

    12. Jan, 2010

    Thanks, just the simple instructions I was looking for!

    Reply to this comment
  4. Theo

    16. Jan, 2010

    Truly brilliant !
    Thanks

    Reply to this comment
  5. Ashfame

    11. Feb, 2010

    Yes you can have multiple sidebars that way but I bet you want them to be widgetized too. Refer to one of my post on my blog – How to have multiple widgetized different sidebars (http://blog.ashfame.com/2009/11/multiple-widgetized-different-sidebars/)

    Reply to this comment
  6. Doug C.

    04. Apr, 2010

    I don’t get it. So you got “” … then what?

    Say I make a widget that I only want to display on certain pages. How would I add that using this “get sidebar” code?

    Now that would be an interesting and useful post.

    Reply to this comment
  7. Amor

    04. May, 2010

    I've been using

    <?php include ('sidebar-single.php'); ?>
    <?php include ('sidebar-page.php'); ?>

    I didn't know I could use

    <?php get_sidebar('single'); ?>
    <?php get_sidebar('page'); ?>

    learned something new again. Thanks Alex!. :-)

    Reply to this comment

Trackbacks/Pingbacks

  1. [...] the sidebar, but instead display a different sidebar. That’s easy if you do multiple sidebars the right way, which, of course, has the following [...]

Leave a Reply

Please use your real name when commenting. Wrap code in <code> tags and make sure HTML is encoded. You can use regular HTML like <a href="... etc.

Get yours questions answered quicker

If you're using this post for paid work and have a question of any complexity then please ask at WPQuestions where you'll get a prompt response.