Create dynamic WordPress sidebars using conditional tags (#)
Note: I have written a post explaining the basics of PHP conditional tags - read that if you are new to WordPress and PHP.
WordPress is a wonderful blogging platform that is very flexible. In this tutorial, I’ll be showing you how to create a dynamic sidebar, which changes according to the current page.
Why Dynamic Sidebars?
Dynamic sidebars help bring your blog to life. They can showcase the important stuff when and where it is needed. For example, on your About page, you want to show client testimonials and wards in your sidebar, but you don’t want to show that stuff when the reader is reading a post. So, you can decide what content to put in the sidebar and when to show it, so you can give items maximum exposure, when it’s required.
The Logic
We use if/else logic (as in PHP) to determine the current page that the visitor is on, and then arrange the sidebar items accordingly. The code looks like this:
IF it is the homepage {
—CARRY OUT THIS CODE—
}
ELSE IF it is the about page {
—CARRY OUT THIS CODE—
}
ELSE {
—DO NOTHING—
}
What the above code does is that it checks to see whether the current page is the homepage. If it is, it carries out the respective piece of code. Else if the current page is the about page, it carries out that respective code. Else, if the page is neither the homepage nor the about page, it does nothing. Simple, eh?
Let’s Start
To determine the current page, we will use a particular conditional tag (see the WordPress Codex). If the page is the homepage, we display recent posts on the blog. Else if the page is the About page, we display a Client Testimonials box.
Else, if the page is neither the home nor the About page, we display nothing:
<?php if ( is_home() ) { ?>
<div class=”recentposts”>
<h2>Recent Posts</h2>
–CODE FOR RECENT POSTS–
</div>
<?php } elseif ( is_page(’<strong>page-id</strong>’) )
<div class=”testimonial”
<h2>Client Testimonials</h2>
–CODE FOR CLIENT TESTIMONIALS
</div>
<?php } else { ?>
–LEAVE BLANK TO SHOW NOTHING–
<?php } ?>
The is_home argument checks to see if the current page is the home page. The is_page(’page-id’) parameter checks for your about page. Substitute the ID of your about page in place of the page-id.
So you see, it’s very simple at that. Instead of showing nothing, you could show anything, in the last part, such as a popular posts section, a poll, even a form. The possibilities are endless.
Inserting Whole Files
It doesn’t stop at puny bits of code. You can insert whole files too, if you want. You do that by including them in the sidebar. For example, taking the above example again, let’s insert a poll.php file instead of the recent posts if the current page is the homepage, and the about.php file instead of client testimonials, if the current page is the about page:
<?php if ( is_home() ) { ?>
<strong><?php include(TEMPLATEPATH.’/poll.php’);?></strong>
<?php } elseif ( is_page(’<strong>page-id</strong>’) ) { ?>
<strong><?php include(TEMPLATEPATH.’/about.php’);?></strong>
<?php } ?>
Now, the code we used to include the files was <?php include(TEMPLATEPATH.'/filename.php');?>. The include part starts the inclusion of the file. The TEMPLATEPATH part finds the path to the your blog’s theme. The ‘/filename.php’ is the name of the file to be included (poll.php, about.php, etc), and the period in between (.) appends the template path and the filename together.
And that’s all you need to do to make your blog’s sidebar dynamic and lively.
Tip
It’s not limited to sidebars only. You can use conditional PHP tags to show or not show content just about anywhere. The logic and code is the same, the difference being the location of the code. Be careful not to overdo using these conditional PHP tags, because these take up processing resources.
Guest post by Muhammad Siyab, who writes blogging, design, SEO and WordPress tips at Putting Blogs First (RSS feed). If you write great content, you too can guest blog on Blog Creativity.
James Paden said on September 9th, 2008
Funny, I just did this myself over the weekend
Sumesh said on September 10th, 2008
@James Paden: Good to hear that more WP users are making use of conditionals
Richard X. Thripp said on September 16th, 2008
is_home and is_page aren’t “parameters,” they’re functions. They’re declared in the WordPress core, specifically, in wp-includes/query.php.
One useful thing is that you can put an exclamation point before them, for example, “if !is_home()”. That shows some code on every page but the home page.
If you have a lot of elseif statements it’s more efficient to use switch / case / break, but that’s a different approach to learn.
The proper (versatile) way to create dynamic sidebars is with plugins and widgets, but that’s complicated. I code conditional statements right into my theme too because it’s easier.
This is a great introductory guide.
Nick said on March 18th, 2009
I’m trying to use the conditional functions to display a different sidebar entirely (which I have successfully registered using register_sidebars() ) but I can’t seem to get the syntax right. PHP fails. I’m new to PHP, so that’s not a surprise, but I’m getting a little frustrated with all these bars and exclamation points. I just want it to say:
if is_home() { dynamic_sidebar(’sidebar 1′);}
elseif is_single() {dynamic_sideba(’sidebar 2′);}
else {dynamic_sidebar(’sidebar 3′);}
THen do I do a endif; or something? Nothing shows up.
Adcantu said on March 20th, 2009
Does this still work? Im having trouble getting mine to work. I am adding the code as listed to my sidebar.php
I can get it to show –LEAVE BLANK TO SHOW NOTHING– but nothing else.
I wanted to include my contact page, page_id=27
here is what I have as my code :
Recent Posts
–CODE FOR RECENT POSTS–
<?php } elseif ( is_page(’page-id’) )
<div class=”testimonial”
Client Testimonials
–CODE FOR CLIENT TESTIMONIALS
<?php } elseif ( is_page(’27’) )
<div class=”testimonial”
contact info
–contact me via the form!
–LEAVE BLANK TO SHOW NOTHING–
what am I doing wrong? Thanks for any help