Looking for a way to create sidebars that look one way on the main blog page, another way on the individual posts, and perhaps a third way on the individual pages? Here’s how to use Wordpress’s conditional tags and the tiniest bit of PHP to do it. Don’t be intimidated, give it a try!
One beautiful thing about Wordpress is that you don’t have to be educated in more than the most basic use of PHP to use its PHP based tags. You just have to learn what they are and how to use them. You can use these anywhere. I’m talking about sidebars here, because they’re often on every page of a blog (home, post, page) but you might want them to look different on different types of pages.
Note: this is for hand-coded sidebars or those using a PHP widget. It won’t work in the Wordpress text widgets.
Conditional Statements in PHP
This is probably easier to learn than you think. PHP allows for conditional statements. They’re written the same way you might give a child a series of instructions. If there’s Cinnamon bread at the store, buy one loaf or else if there’s only raising bread, buy a loaf of that or else buy plain bread. You can include as many else if commands as you’d like and the program won’t get confused like a child might.
Or if it’s an either/or situation, you could write if Grandma is home, give her the cookies or else bring them back here. (No “else if”s if there’s only one “else” needed.)
Make sense?
Using Conditional PHP with Wordpress Tags
Wordpress has a number of conditional tags of varying detail.
For example, if you want something only to occur on Single Post pages, you can use is_single(). Or if you only want something to show up on a particular single post (let’s say the post slug is “awesome-post”) you can use is_single(‘awesome-post’). There are other options to use post number and post title; I prefer slug.
For all Single Pages (not any page, but the ones created as Pages in Wordpress), use is_page(). You can use the same methods as above to narrow it down.
The last one I’ll cover is is_home(). This tag is different from is_front_page(). On the domain blogcrafted.com I’ve set the front page to a static page. The “home” page, however, is the main blog page. On most blogs they’re the same thing, because the front page is also the main blog page.
So unless you’ve been messing in the ‘Settings->Reading->Front page displays’ part of your blog, you can probably use either. On this site, I have to be careful.
For the complete list of conditional tags, visit the Wordpress Codex.
Putting It Together
So how do you put this all together? Let’s take a really simply example. Suppose that you want to put one image at the top of your sidebar on the main blog page, one on single post pages, and one on any other pages (archives/404/pages/etc).
Here’s the code you would use:
<?php if (is_home()) { ?>
<img src="/image1.jpg" alt="Image for the Main Blog Page" />
<?php } else if (is_single()) { ?>
<img src="/image2.jpg" alt="Image for the Single Post Pages" />
<?php } else { ?>
<img src="/image3.jpg" alt="Image for Everywhere Else" />
<?php } ?>
Don’t forget the final php command which closes off the section. It essentially tells the browser “Ok, we’re done with that whole conditional section. The next bits go on every page.”
You can use the above code with simply if and then else. You can use it with as many else ifs as you’d like.
You could even have a different image in the sidebar for every post if you wanted to add all that code to the sidebar. Here’s the complete conditional tag list.
More Advanced Uses (“Not” Statements)
Suppose you want something to only show up on certain pages and don’t want anything there on others. Obviously, you can do this by setting up an if / else conditional statement as above and simply leave one option section blank (don’t put any code or text between it and the next else if/else). But if you would like to set it up to be on every page except one type (or even except one actual page!), you can do that by using a “not” statement.
I should begin by saying that in a lot of coding, “not” is actually done with what’s called a “bang.” And a bang is this: !
Yep, it’s an exclamation point.
So suppose that I want something to show up on every page that is not the home page (so I want it in the sidebars of singe post pages, single pages, 404 pages, the works). Here’s what I write:
<?php if (!is_home()) { ?>
<img src="source" alt="The image that I want visible everywhere but home" />
<?php } ?>
See how the ! is right in-front of the is_home() ? That’s where you have to put it to get the “not” effect. This code tells the browser “if this page is NOT home, then display everything until the end piece of php code.”
You can also set it up so that the item has multiple NOTs. In the case below, this will show up on every page that is NOT “home” and NOT “single.” Just use: && between them and don’t forget the !
<?php if (!is_home()&&!is_single()) { ?>
<img src="source" alt="The image that I want visible everywhere but home & single pages" />
<?php } ?>
You can repeat this ad-infinitum.
Wrapping Up
If you go step-by-step, it’s not too tough. Some people reading this probably said “duh” and others (you?) might be more intimidated. But if nothing else you can start by copying what I have here and go from that.
This type of code can be used anywhere in Wordpress. It’s most useful for sidebars because if you want something only on individual post pages, you’ve already got a theme template file for that. Anything that is used on multiple pages is fair game. Maybe that’s a “navigation.php” file, or maybe it’s your sidebar/header/footer.
It’s also used to set up what will show up on a post/page based on whether or not the comments are open. So keep your eyes open for all the places it can be used. The a list of all the conditional tags helps you see what your newfound edjumication can do.
Later on, I plan to write about the other way to have varied sidebars. It simply involves making a number of sidebar files (or having several complete sidebars in one file) and calling different ones in different pages of your Wordpress theme. The advantage of the method I’ve outlined above is that you can do it entirely in one file and it’s good for minor changes.
If you've found this article useful, why not get new posts in your RSS reader or in your e-mail? Your e-mail will only be used for new posts and you can unsubscribe at any time.




{ 1 trackback }
{ 7 comments… read them below or add one }
Perfect! If only the codex had been this easy to walk through!
This was very helpful, thanks. Do you know of a way to exclude certain category names from displaying in the the_category() function? I use a special category for subscriber only content and I won’t want that name to display in the category list on the post if possible.
That’s a good question, Bill. I don’t think that the_category() has an exclude option like listing categories does. Checked the codex too and didn’t see one.
But if this category only shows up on posts that are in it (which is what the the_category() call is for), then shouldn’t only people paying for premium content–if that’s your subscriber model–be able to see that post anyway? It’s not perfect because they’ll still see it, but if you name it something like “Premium content” then I don’t think it’ll look bad.
I decided it probably won’t work. I would have to query the list first and then trim the ones I want out, which I think would be prohibitively slow. We are just going to stick with the “premium content” category and not worry about it. Thanks though!
Thanks. I am trying to get a tag cloud to display everywhere on my site (pages, archives, main page, posts etc. )
What language do I use and do I insert it in the ‘widget logic’ section of the tag cloud?
Hi Simone,
I’m not familiar with the backend of that particular widget. You may be able to find instructions in its readme.txt file (should be in the plugin folder). Also try seeing if it appears everywhere by default when you don’t set parameters.
MsC
Thank you!!
I’m not a coder and was hard to get different header for a static page that is also a posts page.
”Not” Statements is what I learned today.
Thank you again!
Marcelo’s last blog post..Infografia: Uma força visual para a compreensão