Using a Second Sidebar for Different WordPress Pages

This post was originally written when I was using a different theme. That doesn’t affect the content, just wanted to let you know that my current theme (designed by Ryan Burrell) doesn’t use this particular method. Rather, it employs various templates to display bars as necessary. For more information, check out my guide How to use page templates in WordPress.

I wrote sometime ago about the visual design of a website, particularly a WordPress blog, and lamented the long sidebar that plagues so many sites. Like anyone else, I want to show everything I have to offer to my readers, but I know that my readers don’t necessarily want the same. End-users want the information they want and nothing else that might distract them from that information. Readers want data to be concisely presented and easy-to-read. A sidebar as long as my arm detracts from these goals.

Therefore, when I added some things to my WordPress sidebar (like a blogroll) that made it particularly long, I knew something had to be done. It’s all fine and well on the main page where ten blog entries make for a rather long site (and therefore the sidebar doesn’t look out of place). But when looking at a single post, the sidebar added a dozen inches below the comment box until you reached the footer. No good.

The WordPress Codex has some tips on creating a second sidebar and using this for other pages (such as when viewing just a single post), but beyond giving me the PHP tag for the sidebar, it didn’t do me a lot of good. The theme I use did not have the sidebar statement in the default location (which is normally sidebar.php in your theme’s files), and when I did find the code I needed to change, the tag listed in the WordPress Codex didn’t work.

My sidebar actually extends up from the footer, so it was the footer.php I needed to edit. Removing the standard sidebar tag, I entered the following:

When I first made this change, I discovered that, while it worked great for posts, my pages still had the longer sidebar. Since my Contact page is pretty short, this ran into the same problem as before: the page content had ended, but the sidebar just kept going. Therefore, the code needed to be modified slightly to accommodate pages as well as single posts.

Using an elseif statement, we are able to use sidebar2.php on both pages and single posts. If you have additional templates on which you wish to use a different sidebar (say you wanted a different sidebar on the category, archive, and portfolio pages as well), you might considering using a switch, but with my limited knowledge of PHP, using if/elseif/else statements worked just fine for me.

10 thoughts on “Using a Second Sidebar for Different WordPress Pages

  1. This is exactly what I was looking for! How might I use this to send one sidebar to multiple pages? Is that possible?

    I’ve tried this and it didn’t work…

    Thanks for the post. 🙂

    1. You caught me just as I’m heading out of town for two weeks, so I don’t have time to test it, but what you’d do is build custom templates for those pages. The WordPress Codex has a guide on using custom templates, but it’s a little confusing (as most everything in the Codex is). I’d be happy to write a follow-up article to address your question, but it’ll be next week at the soonest and probably later.

  2. Thanks so much for the quick response. I’ll make separate sidebar files in the meantime but I have several pages and would prefer to know how to make it work with less files so if you have the inclination someday when you have time that would be great.

    Just as an FYI the way I’m doing it is using the code you have above in the page.php (replacing the default sidebar template call) and separate sidebar.php files. I’m doing it this way because we want different optin offers on different pages. Some pages could share one sidebar but I’m just not sure how to make it recognize mulitple page IDs for one sidebar call. Hope that makes sense.

    Thanks again!
    Melanie

    1. Just wanted to let you know that I was finally able to do a write-up on using different templates in WordPress to achieve what you were looking for. Rather than having a sidebar set to push to different pages, as you considered, it’s best to create a template that calls different sidebars.

      Check out How to Use Page Templates in WordPress for more information.

  3. You could also use a plugin – like the Graceful Sidebar Plugin to customize the sidebar content for each page or post. This very simple plugin extends the post/page edit screen by adding two additional fields – one for the title and one for the content of the sidebar widget. When your post or page displays, so too will the sidebar widget content.

    Enjoy!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s