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

18 Responses to “Wordpress – Using a different sidebar for pages and single posts.”

  1. santaram (1 comments.) Says:

    thanks for adding my blog into ur techno fav and please feel free in visiting my blog

    http://santa-reviews.blogspot.com


  2. Malin (36 comments.) Says:

    That method is actually easier for noobs than for example this method is so Yay to you for posting it :)


  3. dude (16 comments.) Says:

    cool man, Mike is a PHP guru now :D


  4. Jenny (39 comments.) Says:

    Awesome. :) I’ll have to remember that when I do my site.


  5. Jenny (39 comments.) Says:

    I linked you on my new site :)


  6. Mike Says:

    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.


  7. Mike Says:

    I shall link back to you :D


  8. Mike Says:

    Oh yesss. I’ve mastered the php include ;D


  9. roxzen (2 comments.) Says:

    You are good in php! Guess I’ve a lot to learn from you in future. =D


  10. Mike Says:

    Hehe…well I dunno if I’d say that. The php include just seemed like a logical answer :D


  11. A little link love - Week Four » Ordinary Folk Says:

    [...] 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 [...]


  12. Wordpress - Using a different sidebar for pages and single posts. - Bloggst Forums Says:

    [...] 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 [...]


  13. Andrei (1 comments.) Says:

    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,


  14. Multiple sidebars in Wordpress | ITchurch.com Says:

    [...] 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 [...]


  15. reg (3 comments.) Says:

    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


  16. reg (3 comments.) Says:

    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


  17. reg (3 comments.) Says:

    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


  18. Eli Says:

    Hi!
    thanks for the trick.
    How can I write the condition: if is category id=2 get the sidebar2?

    Many thanks!


Leave a Reply

Please note: All first time comments are moderated and will show up after admin approval. After that all comments you make on Things by Mike will not be moderated. Furthermore if you come here to make a comment just so you can leave a keyworded link, KNOCK IT OFF. Your comment WILL BE DELETED before it ever shows up on this site, so stop wasting your time and mine. If you want advertising here, you can pay for it, like others appropriately do.

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

CommentLuv Enabled