Multiple sidebars in Wordpress
First of all, I just want to thank Carly for the suggestions how to make the site look a bit better. I have increased the content’s padding and aligned the sidebar content to the right rather than the left. The design does look better like this, however I don’t like the look of the gap in between the left content and the right. It just looks… funny. I thought about adding a line in between them, but that’ll look stupid. Any suggestions?
Back to the topic, I have noticed lots of people asking on other sites how to add different sidebars depending on the page on their websites. This is a trend that’s actually good since adding multiple sidebars to sites can make the content displayed in a more organized fashion. If you view my “About Me” and “Site Info” page, you will find that the sidebars change. It’s very simple to do as explained in Jem’s blog entry about this. However, if you use Wordpress like me, it gets a little tricky. There are various methods of adding extra sidebars to Wordpress, but I’ll show you the method I have used.
First of all, create the extra sidebar files you would like. I just copied and pasted my current sidebar content into Notepad and edited it depending on what page it was for. Name these files something like sidebar2.php and sidebar3.php and upload them to your current theme’s folder. If you want to have more sidebars, create more sidebar files. Once you have done this, you’re ready to edit the coding.
In Wordpress for your content pages, you have one main template which is usually called page.php. You’ll notice the PHP coding calling the sidebar file. It is near the bottom of the page depending on what type of theme you use. It looks like this:
<?php get_sidebar(); ?>
Change the code to this:
<?php if (is_page(n)) {include ('sidebar2.php'); }
else if (is_page(n)) {include ('sidebar3.php'); }
else {include ('sidebar.php'); }
?>
This example uses three different sidebars. If you wish to add more, just add extra “else if” lines. You need to find out what ID numbers your content pages have. In your wordpress control panel click “Manage” then click “Pages”. You will then see all your pages listed with their ID’s. Suppose sidebar2.php is for your “About Me” page and the page ID is the number 2, where the “n” in is_page(n) is you’d put the number 2. Do the same for the other sidebars then save and upload page.php. The sidebar.php at the end of the code is the default sidebar if a different one isn’t declared. Et voila! You should have different sidebars depending on the page.
This is my first tutorial blog entry, I decided to take the plunge and have a go at them. Hope this is helpful somewhat.


Carly
January 18th, 2008 at 6:56 pmFirstly, thanks for the link! Secondly, I love this layout: very nice! and Thirdly - thank you for this tutorial as I was wondering how to do this!