By using simple conditional tags – it’s pretty easy to add some very basic hacks to your Wordpress theme to have more control over what’s displayed when.
Here are some things you could do with a conditional tags:
- Display something only on certain pages
- Display something only on certain categories
- Display something in header and footer only at certain times
- Display something only on sub-children of particular pages
- Display something only in the WP dashboard
- Display something in the sidebar only when certain conditions are met
- Do something only when there’s a “sticky” post
- Do something only when a “page template” is used
- Do something only for “author pages”
- Display something only on search or 404 pages
Let’s say you want something to display only on the homepage, or just category pages, or maybe just your 404 (not found) page – it’s quite easy to do. You don’t have to be a hardcore programmer (I’m not for sure) to implement these very simple theme hacks.
Where to Use Conditional Tags
It all depends on your your theme is structured. I’ve seen Wordpress themes that a single “index.php” file handle just about everything, and other ones that use only the home.php, index.php, single.php, archive.php, and category.php files. You could handle everything with a bunch of code in one file if you want using conditional tags, have individual files for each thing, or any combination in between.
Most themes I’ve encountered usually have an index.php and a single.php only. If you want to know what pages Wordpress looks for first in a theme before defaulting to the “index.php”, read the official Wordpress Template Hierarchy page.
Conditional tags are great because you can use them both in and out of the loop. You can use them directly in theme pages, but you could also use them in your header, footer, comments, and sidebar files. Actually the sky is the limit, and you have only your own imagination to limit you!
Conditional Tag Examples
The worst thing I see on most posts about conditional tags is that they don’t have a lot of examples for you to draw from – so I’ll try to give you a few ideas to get you started…
If This is XYZ page
One of the most common ways to use a conditional tag is to add a filter of sorts to tell Wordpress “if I’m on ‘XYZ’ WP page – then do this. The most common reason would be maybe to show certain things (ads, text, messages, graphics, scripts, flash) in certain places.
For example, let’s say you want to display a message to visitors on your home page only…
<?php if (is_home()) { ?>
Welcome, you will only see this message on my homepage!
<?php } ?>
replace “is_home” with “is_front_page”, “is_single”, “is_sticky”, “is_page”, “is_page_template”, “is_category”, “is_tag”, “is_author”, “is_date”, “is_archive”, or “is_attachment” to make your message show up on nearly any Wordpress page. Remember, once the condition is met, you can “do” anything – from including a file to showing special graphics, running a script, anything! You could use this condition for example to show an ad on just your homepage, or just single pages.
If this is XYZ page show this, else show that
The nice thing about conditional tags is the fact that you can have as many conditions as you want…like this:
<?php if (is_home()) { ?>
<p>Show this!</p>
<?php } elseif (is_single()) { ?>
<p>Show this instead!</p>
<?php } elseif (is_category()) { ?>
<p>Show something different!</p>
<?php else { ?>
<p>Show this if no conditions are met</p>
<?php } ?>
OR – you could even structure it in such a way that you lump some conditions together like this:
<?php if (is home() || is_single() || is_category() || is_page()
|| is_archive() { ?>
<p>Show this on all those pages!</p>
<?php } ?>
The double-pipe or || in the code signifies “OR”, so Wordpress knows, if this is home, or a single page, or a category page, or a “page” page, or an archive page – then so something.
Show Everywhere, Except…
Sometimes you want to show something everywhere possible except just one or two places…
<?php if (is_home()) {
}
else { ?>
<p>Show this everywhere!</p>
<?php } ?>
With this code we just say if it’s “home” do nothing, else show do this. You could add multiple conditions (exclusions) to this using the || OR operator as in previous examples.
Getting even more specific
One thing I hadn’t mentioned was that you could pass additional parameters to the conditional tags for even finer grained control. For example, instead of targeting are single post pages with “is_single”, you could actually target just one using any of these formats:
is_single(‘25′) // uses posts ID
is_single(‘Title of my post’) //uses the exact title of the post
is_single(‘title-of-my-post’) //uses the permalink of the post
is_single(array(25,’this title’,'this permalink’)) //uses when any of the 3 are true
You can use similar parameters for paged pages, template pages, categories, tags, etc. The official Wordpress Conditional Tag page in the Codex lists them all.
Force Wordpress Functions for Certain Conditions
Have you ever not wanted to add people to your blogroll because their link will display on EVERY SINGLE page of your WP powered site? That’s an easy hack with conditional tags, because you could hack your sidebar to display your blogroll ONLY on your homepage like this:
<? php if (is_home()) {
wp_list_bookmarks();
}
?>
You could modify this to display just about anything in the sidebar for whatever conditions you want. Let’s take this a bit further though – let’s say that maybe I want a special header or footer when certain conditions are met? You can do that too…
<? php if (is_home() || is_single() || is_page()) {
get_header();
}
elseif (is_category() || is_tag()) {
include (TEMPLATEPATH . '/header2.php');
}
elseif (is_404() || is_search())
include (TEMPLATEPATH . '/header3.php');
} ?>
Using that example code you could have as many different headers, footers, or sidebars as you wanted and you could include them for whatever conditions you specified. Just replace get_header with get_footer or get_sidebar, and edit the rest include the files you want.
Using Conditional Tags to Change Styles
So far my conditional tag examples have been to show you how code conditions to do this or include that. Another very simple (and powerful) was to use conditional tags is to just use them for coding style. You could have any element (paragraph, div, heading) change colors or font size or anything related to style when certain conditions are met.
Let’s take a very simple example, maybe your pages have content contained within one div like this:
<div id="content" class="main">
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<?php endwhile; ?>
</div>
In most themes, that main “div” is styled the same way on EVERY single page of the theme. Maybe you want it styled one way for your homepage, but another for paged pages, single posts, archives, etc. What you do in this case is to write different classes in your stylesheet for each and call them something like “single”, “archive”, and “paged” – and then you code it like this:
<div id="content" <?php if (is_home()) { ?> class="home"
<?php } elseif (is_page()) { ?> class="paged"
<?php } elseif (is_single()) ?> class="single"
<?php } elsif (is_archive()) { ?> class="archive" <?php } ?> >
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<?php endwhile; ?>
</div>
You use any variation of this to control any element of your theme at will under nearly any condition. This would also be a great way to control your Post title and meta and have it display different things on different parts of your Wordpress powered site.
Conclusion
I think mostly that the power of conditional tags is widely underutilized. Many of us just take for granted the way a theme works and looks, and forget that with just a few strokes of code and some imagination – we can change just about anything under the sun in our Wordpress theme! I’d like to see some comments on what kinds of things you’ve used conditional tags for in your Wordpress site (code samples welcomed!).
This post was submitted by John Pratt, who writes about how to Make Money Blogging. His latest post is How to Build a Wordpress Craigslist.


















For controlling widgets one could also use Widget Context or Widget Logic plugins, which don’t require fiddling with code.
Nice post @Jhon. thanks i will try to use this.
Very informative and well explained, I will be trying out the Conditional Tags to Change Styles on different pages.
Thank you so much…
Regards Micheal
Another good one would be to set a conditional in your title tag. If it’s the home page, set the title to the name of the blog and the description. If it’s a post page, set it to the title of the post. This would help SEO.
Excellent wordpress hack. Best of all, the examples!!!
thanks, this is very useful, but i little confused with it..
but, i will try for this…
John thanks for an awesome and really relevant post – am not really much of a php coder and have been looking for examples exactly like this to give me a start with the if-then-else structure in php (especially revelant because I need to show different feed subscription forms based on diff blog categories)
Another question although not related to this I’m hoping you can suggest something for – do you know of any wp plugin I can install that people can use to increment a counter (i.e. like a voting thing but it SHOULD allow a person to vote more than once – the idea is I want to put a laugh counter on the site to see/ show how I’m doing with my ‘get 1 million laughs’ goal).. any suggestions as to what might work will be most helpful! Thanks
Highly useful! I’ve got to the stage where using a snippet of code is far better that using a plugin. Plugins are great, but ultimately too many clog up the works and slow the blog. I used widget logic, but I now prefer to hard code the theme and keep those queries down!
Very useful tips. Got to try some of them.
Hello there, I have been going crazy trying to have assigned different images to the header but without any luck. The problem I have is that I don’t know how to implement these code.
1. So I made different files header2.php, header3.php etc (is there something I have to change or include in these new files?)
2. I have the original header.php file (were do I have to put these php code… within the •••here•••… instead of the “id header” tag… or on the first line of the documet.
3. Do I need to add anything to my CSS file?
Thanks for any help.
Awesome post! Expertly written – great show of examples John. Bookmarked, Stumbled and Dugg!
So, assuming you (as admin) make most of the posts, but you have occasional contributors; then with a conditional tag you could display the gravater of the author when (condition) it’s not you? You don’t want your own avatar to appear every time do you!
Great tips, they will definitely come in handy. =)
thanks, what a nice trick
Thanks John! I’ve been burning hours trying to bring in a separate header for a static front page in Wordpress 2.8 and this did the trick in a few minutes. And — bonus! — also gives me a way to easily bring in different sidebars. You rock!
Very informative and well explained, I will be trying out the Conditional Tags to Change Styles on different pages.
Thank you so much…
joe
Do you know how I can change the class of my post title when the character exceed a certain number (16 as example) but using a function ?
I have already used a conditional to display my post title as h2 in index and h1 in single, and apparently it’s not possible to use conditional in conditional …
Very informative…
But… How would I have it follow two conditions? For instance, I would like to have multiple loops on my archive page. Each loop is to show only the posts that have specific tags, but withing only the category I am in.
I tried : if (is_category() && is_tag(‘mall-kiosks’)); but that didn’t work…
This was what I needed. I didn’t have to search more than 2 links to find everything all on your site! Thanks