Feedback Form

Skip to content


Using a Second Sidebar for Different WordPress Pages

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 10 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, 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:

<div id="sidebar">
<?php if (is_single()) : ?>
 <?php include ('sidebar2.php'); ?>
<?php else : ?>
    <?php get_sidebar(); ?>
<?php endif; ?>
</div>

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.

<div id="sidebar">
<?php if (is_page()) : ?>
    <?php include ('sidebar2.php'); ?>
<?php elseif (is_single()) : ?>
    <?php include ('sidebar2.php'); ?>
<?php else : ?>
    <?php get_sidebar(); ?>
<?php endif; ?>
</div>

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.

Share and Enjoy:
  • Google
  • del.icio.us
  • MySpace
  • Facebook
  • Digg
  • StumbleUpon
  • TwitThis
  • Slashdot
  • Technorati
  • E-mail this story to a friend!

Related posts:

  1. How to Edit your Single Post Navigation on WordPress
  2. WordPress not quite ready for mobile browsing
  3. Carrington 1.3 has been released
  4. WordPress-MU 1.5.1
  5. Wordpress Advanced Editor

Posted in Technology. Tagged with , , , , , , , , , .

0 Responses

Stay in touch with the conversation, subscribe to the feed for comments on this post.

Some HTML is OK

(required)

(required, but never shared)

or reply to this post via trackback.