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!

Comments

  1. 1
    santaram (1 comments.) // April 23rd, 2007

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

    http://santa-reviews.blogspot.com

  2. 2
    Malin (34 comments.) // April 23rd, 2007

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

  3. 3
    dude (16 comments.) // April 23rd, 2007

    cool man, Mike is a PHP guru now :D

  4. 4
    Jenny (39 comments.) // April 23rd, 2007

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

  5. 5
    Jenny (39 comments.) // April 23rd, 2007

    I linked you on my new site :)

  6. 6
    Mike // April 23rd, 2007

    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. 7
    Mike // April 23rd, 2007

    I shall link back to you :D

  8. 8
    Mike // April 23rd, 2007

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

  9. 9
    roxzen (2 comments.) // April 24th, 2007

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

  10. 10
    Mike // April 24th, 2007

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

  11. 13
    Andrei (1 comments.) // September 14th, 2007

    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,

  12. 15
    reg (3 comments.) // May 24th, 2008

    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

  13. 16
    reg (3 comments.) // May 24th, 2008

    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

  14. 17
    reg (3 comments.) // May 24th, 2008

    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

Trackbacks

  1. A little link love - Week Four » Ordinary Folk
  2. Wordpress - Using a different sidebar for pages and single posts. - Bloggst Forums
  3. Multiple sidebars in Wordpress | ITchurch.com

Leave a Comment

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.