Wordpress - Using a different sidebar for pages and single posts.
Posted By Mike on April 23rd, 2007
Sorted in Wordpress
As I mentioned in a previous post I recently installed Wordpress locally on my machine, so I could experiment and test changes without breaking this live version of my site. One of the first things I wanted to do was use a different sidebar for my pages. I got thinking about it for a bit and I figured why couldn’t I just make a second sidebar file and use a php include to insert it into the page template? Sure enough that works. Here’s the basic process I went through in creating and applying the alternate sidebar. Maybe there’s a better solution, but this works for me.
First you’ll need to create the new sidebar. What I recommend doing is just creating a new file and filling in what elements you want to appear. Here’s what my second sidebar’s code looks like:
<div id=”sidebar”>
<ul>
<li class=”sidebox”>
<h2>Gummy Stuff</h2>
<ul><center><?php randomSpreadArticle(medium)?>
<?php randomSpreadArticle(medium)?></center></ul>
</li>
<li class=”sidebox”>
<h2><?php _e(’Categories’); ?></h2>
<ul>
<?php
if (function_exists(’wp_list_categories’))
{
wp_list_categories(’show_count=1&title_li=’);
}
else
{
wp_list_cats(’optioncount=1′);
}
?>
</ul>
</li>
</ul>
</div>
As you can see I’ve included two things. The first is “Gummy Stuff” which will be random Gummy goodies for sale on Spreadshirt. The second is obviously the listing of this site’s categories. You can add as many things as you’d like. One important thing to remember - if you’re using widgets with your sidebar, you’re going to need to remove the dynamic sidebar php code. If you don’t then you’re new sidebar will just default to the widgets you’re using and it will look just like the sidebar on your main page. Just look for these two lines and remove them.
<?php if ( function_exists(’dynamic_sidebar’) && dynamic_sidebar() ) : else : ?>
<?php endif; ?>
Once you’re done including all the elements for your new sidebar, save it as “sidebar2.php” or something similar. Now open up your page template and look for the following.
<?php get_sidebar();?>
…and replace it with
<?php include (’sidebar2.php’); ?>
You could also do the same thing for your single posts. Same idea…just open up your single.php file and do the same thing you did for the page template. Hopefully someone will find this helpful ![]()
Popularity: 20% [?]
Looks like you've never been here before! Today is your lucky day. For just the price of a simple mouse click, you can subscribe to this blog's RSS feed!
Related Posts
Comments
Trackbacks
- A little link love - Week Four » Ordinary Folk
- Wordpress - Using a different sidebar for pages and single posts. - Bloggst Forums
- Multiple sidebars in Wordpress | ITchurch.com



thanks for adding my blog into ur techno fav and please feel free in visiting my blog
http://santa-reviews.blogspot.com
That method is actually easier for noobs than for example this method is so Yay to you for posting it
cool man, Mike is a PHP guru now
Awesome.
I’ll have to remember that when I do my site.
I linked you on my new site
That version is pretty interesting though. You could actually have a different sidebar depending on what page you visit. That’s interesting…hmmm
But yeh I was going for the absolute noob approach..hehe.
I shall link back to you
Oh yesss. I’ve mastered the php include ;D
You are good in php! Guess I’ve a lot to learn from you in future. =D
Hehe…well I dunno if I’d say that. The php include just seemed like a logical answer
Hey man,
I would like to have the sidebar appearing on all the pages.
And even better, I would like to have one sidebar in the left and one in the right.
I am now using the default theme. It’s look & feel it’s satisfatcory to me, apart from some minor complaints.
Anys suggestion on how to do those modifications to sidebar?
Thanks,
okay, for the longest time i could not get your code to work even though i followed your instructions to the letter… i was copy-pasting the last bit of code you gave on your entry and would always get a fatal error which was weird because the code looked right… until i copy-pasted a similar code from my own wordpress template and saw the difference between your code and theirs:
<– wordpress code
<– your code
the difference is in the single quotes… dunno, but once i got the quotes right, it worked for me like a charm… anyway, thanks for this
reg’s last blog post..Uploads
okay that didn’t work… lemme try again
<?php include(’sidebar2.php’); ?> –this is wordpress code
<?php include (’sidebar2.php’); ?> –this is your code
reg’s last blog post..Uploads
argh whatever. this is making me look stupid
anyway, i probably should’ve typed the code myself instead of copy-pasting in the first place huh. your post formatting might’ve gotten the single quotes to display incorrectly that’s why PHP/wordpress couldn’t handle the request
reg’s last blog post..Uploads