The Dynamic Sidebar

March 18th, 2008

Having a dynamic sidebar is a great way to make your website more user-friendly! You can display extra content to enhance your pages or provide additional navigation links for sub-sections. Since the release of my new design, I've had a few people ask how I made my sidebar change on each page using Wordpress.

The Dynamic Sidebar

I am going to attempt to walk you through the entire process that I used to create the effect. But before I dive into that abyss (because it truly is one), I want to mention that this is an advanced way of manipulating code in Wordpress and is not recommended for beginners unless you do some additional research.

There is also a snippet of example code for a dynamic sidebar on the WP Codex, but it might be easier to understand here because you can build it along with me.

We will be utilizing Wordpress's Conditional Tags and Template Tags on our sidebar.php template page. This is the only template page in your theme folder that you will need to edit. It may get quite long code-wise (mine is about 200 lines), but have no fear, I will also show you how to organize it all as well. :)

For this walkthrough, I will be using my website (this one!) for most of the page structure and examples. At the end, I will provide you with a downloadable version of the advanced sidebar we will construct using the conditional tags. However, you are welcome to download it now to play with as we go through the steps.

Got it? Ready to go? Woot, let's get to it. I would advise you now to open up the "Tags" pages I linked above in new windows (or tabs) so you can easily reference them throughout this whole thing.

Basic Structure

The first thing to do is to create little placeholders in the sidebar for our content. Utilizing the conditional tags (of which I am going to assume you have looked over), and some PHP magic, we can write this like so:

<?php if (is_home()) { ?> Home content. <?php } ?>
<?php if (is_single()) { ?> Single post content. <?php } ?>
<?php if (is_archive()) { ?> Archive content. <?php } ?>
<?php if (is_search()) { ?> Search results content. <?php } ?>
<?php if (is_page()) { ?> All pages content. <?php } ?>
<?php if (is_page('about')) { ?> About content. <?php } ?>

The is_home() tag tells Wordpress that the content in between the { brackets } should only be displayed on the homepage. The is_page() tag declares the content inside to show only on a page. Please remember to keep <?php ?> wrapped around the opening and closing of each function.

Post or Page Slug

To specify which page to display it on (in this example the page is "about"), you will need to add either the page's ID, title or slug in between the parentheses, as displayed above. I am currently using the page slug, "about", because that seems easiest to me.

Read up on the other tags (archive, single, search, etc) to familiarize yourself with what type of page will display. You are free to play around with my website to see what content pops up on the sidebar while viewing different pages. Try browsing using my sitemap or type in a random search query in the box on the top-right. :)

You could honestly stop at the code above and just plug in what content you want for each specific page. But, for example, if you are wanting certain content to appear on the homepage and the archive pages but not on the search results page (or some similar structure), then please continue reading. :)

Pages: 1 2 3 4 5

Comments

Would you like to add one?

Mar 18, 2008 @ 12:12pm

AHHHHH! I’m just coming to the end of figuring out conditional tags for my new layout (couldn’t you have written this last week?!), this is amazing, thank you! Notepad++ sounds exactly like what I need with my tendancy to miss off brackets…
Anyway, thanks very much for this, it’s really helpful!

Mar 18, 2008 @ 12:12pm

LOL! No problem! And I actually had written most of it a couple weeks ago, I just slacked on finishing. :P But I hope it helps you tie up any loose ends! :D

Mar 18, 2008 @ 1:01pm

Ohw… thank you so much ^^. The sidebar I have right now uses div-styles at the moment, so I guess I will to covert this to h1,h2,h3 etc. first? And then I can try to do something with these things? It all seems a bit difficult but maybe with some help (… okay, lots of help :P) I might be able to do it. Thanks anyway for sharing this!

Mar 18, 2008 @ 1:01pm

You shouldn’t have to convert anything! :) You can just move what you want to show between the closing and ending brackets { } in the PHP code.

I know you’ve just switched over to WP recently, Marieke, so don’t rush into anything too quickly! All this stuff can get pretty complicated and confusing pretty fast. But we are here to help you! :D

Mar 18, 2008 @ 2:02pm

Yay! I was just about to go looking for a tutorial for this exact thing! I’m glad that I can use your tutorial. That way I can ask you questions if I’m lost! lol I will have to come back to it later though. :(

Mar 18, 2008 @ 3:03pm

Ok well I learned ONE new thing. xD Thanks for this.

Mar 18, 2008 @ 4:04pm

That way I can ask you questions if I’m lost!

You are always welcome to! :D

Ok well I learned ONE new thing.

Only one? XD

Mar 18, 2008 @ 6:06pm

Oh my, Melissa. Thank you so very much. I have been waiting on this. I think :) I can’t wait to start fiddling with this I am getting excited about new templates for my sites. Thanks again. I don’t know what I would do without your resources or your help.

Mar 19, 2008 @ 2:02pm

OMG thats a wonderful tut on this when i redo my skin i try this *crosses fingers that this dummy can get it* …… thanks so much

Mar 20, 2008 @ 7:07am

You all are quite welcome. :)

Mar 20, 2008 @ 8:08am

Hmm, I see you use “or” instead of “array”, like the conditional tag page on Wordpress suggests. Why is that exactly (’cause atm for some reason “array” won’t work for me, but it does save a bit of typing…)

Mar 20, 2008 @ 8:08am

The reason why array doesn’t work for you yet is because it is a Wordpress 2.5 feature. I may modify this walkthrough once it’s released and I can play around with it more.

Mar 20, 2008 @ 9:09am

Ugh, these tricks wordpress plays.
This also does the same thing as the “or” though, right, just without the extra ()?

<?php if (is_home() || is_single()) { ?>

Mar 20, 2008 @ 9:09am

That’s how I did my changing footer (I have that info my footer)… with the difference that I used a switch statement and the pages which make use of this are the ones not powered by wordpress :P

Mar 20, 2008 @ 9:09am

This also does the same thing as the “or” though, right, just without the extra ()?

You can use || but you need the extra parentheses. :) You can use any other logical operators (to my knowledge) to connect statements but I didn’t go over them to keep things simple.

Mar 21, 2008 @ 9:09am

This is a wonderful and useful tutorial. My method was getting very complicated so I have changed to this method. Thanks so much for sharing this with us, Melissa. :D

Mar 21, 2008 @ 10:10am

You’re welcome, Sarah! :D

Mar 21, 2008 @ 11:11am

Ick, just realised on the actual child pages the navigation doesn’t appear. Do I need to make extra tags for them? I did make tags for their parent pages.

Mar 21, 2008 @ 11:11am

Ah, sorry. Figured it out! Silly me. :D

Mar 21, 2008 @ 11:11am

It’s always more fun to figure things out yourself, isn’t it? XD

Apr 18, 2008 @ 7:07am

Thank you - very handy! I know I’ve used this tutorial once before, but I’ll be reaquinting myself today, and using it again!!

Apr 20, 2008 @ 9:09am

Okay, how would I do this:

I want a certain sidebar on all my pages EXCEPT for the single page, where I would have a different sidebar. Would I have to copy the content for each one, or could I do something like if() else()?

Thanks. :D

Apr 20, 2008 @ 10:10am

You could definitely use the conditional statements for that! :) The particular way to do it would be under the “Working with Subpages” section in this walkthrough, but this is how you would achieve it for your particular need:

<php if (!is_single()) { ?>

Content to appear if NOT single page.

<?php } else { ?>

Content to appear if IS single page.

<?php } ?>

The exclamation point added to the first statement means “not this”, generally speaking. I hope that helps!

Apr 27, 2008 @ 1:01am

I know I am missing this somewhere…. and instead of emailing you & all & not letting my idiotcity(sp) show… how do I stop the showing of the child’s child page. Like domain-reviews-review by… I want the review by not to show. Purty please show me the light.

May 02, 2008 @ 10:10pm

Jessica, I’ve finally answered your question on my Dynamic Subnavigation walkthrough! XD It is under the “subpages of subpages” section.

I’ll email you to make sure you get your answer. :)

Post a Comment

Your response may be added to a moderation queue if you have never commented before or your comment contains more than one link. If it has not appeared after a day, please contact me so I can check if it was marked as spam. Thank you! :)

Please quote people and converted code with <blockquote>. Allowed formatting includes: <strong>, <em>, <strike>, and <abbr>. You may use links that are friendly ones. :)

-_-; :P :D X( :goodie: XO :/ :no: :( X_X 8| :wah: <3 :ohyeah: ;) :| :) :dance: :X :O :cheer: :nod: O_o ^_^ >_> :$ XD :T :B ^^;