>echo linux

echo> First Thoughts on Aquaria and Reinstalling WoW - http://bit.ly/8XRhLD 1 hr ago
About | Recommended Hardware | Contact

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

<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, usingif/elseif/else statements worked just fine for me.

If you liked this post, please share it with all your friends!
  • Reddit
  • Digg
  • Slashdot
  • Technorati
  • Google Bookmarks
  • Live
  • Facebook
  • StumbleUpon
  • Netvibes
  • del.icio.us
  • RSS

Category: How-To, World Wide Web

Tagged: archive, contact, pages, php, sidebar, template, wordpress

Leave a Reply

echo> newsletter

Never shall spam be sent.

About

If you've ever gone looking for help about how to do something with Linux, you know what it's like to try search after search on Google, find a good forum, and then go through half a dozen threads and maybe on to multiple forums to find the answers you need.

This site's goal is to help detail the process, instead of just answering the single question. It's the confluence of three things I love: Linux, the community of helpful people who use it, and writing articles so everyone can more easily use Linux.

Read more...

Recent Forum Posts

Archives