Wordpress – Using a different sidebar for pages and single posts.
Apr 23, 2007 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: 18% [?]
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!







April 23rd, 2007 at 1:38 am
thanks for adding my blog into ur techno fav and please feel free in visiting my blog
http://santa-reviews.blogspot.com
April 23rd, 2007 at 9:05 am
That method is actually easier for noobs than for example this method is so Yay to you for posting it :)
April 23rd, 2007 at 9:17 am
cool man, Mike is a PHP guru now :D
April 23rd, 2007 at 11:21 am
Awesome. :) I’ll have to remember that when I do my site.
April 23rd, 2007 at 11:26 am
I linked you on my new site :)
April 23rd, 2007 at 3:25 pm
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.
April 23rd, 2007 at 3:26 pm
I shall link back to you :D
April 23rd, 2007 at 3:28 pm
Oh yesss. I’ve mastered the php include ;D
April 24th, 2007 at 1:44 am
You are good in php! Guess I’ve a lot to learn from you in future. =D
April 24th, 2007 at 1:51 am
Hehe…well I dunno if I’d say that. The php include just seemed like a logical answer :D
May 16th, 2007 at 11:21 am
[...] is only displaying your blogroll on your main page. This is something I’m already doing, but I use a little different method. If you’re new here, please consider subscribing to our RSS [...]
July 6th, 2007 at 5:45 pm
[...] Wordpress – Using a different sidebar for pages and single posts. 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 [...] Read the full post [...]
September 14th, 2007 at 6:06 am
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,
February 8th, 2008 at 10:14 am
[...] in the new itchurch.com layout, cause our post pages are usually much longer then our main page. This article gives you the know-how to do it [...]
May 24th, 2008 at 4:36 pm
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
May 24th, 2008 at 4:39 pm
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
May 24th, 2008 at 4:42 pm
argh whatever. this is making me look stupid :P
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
October 30th, 2008 at 6:08 am
Hi!
thanks for the trick.
How can I write the condition: if is category id=2 get the sidebar2?
Many thanks!