A conditional tag can make the browsing experience for your visitors a little different depending on which page they are browsing. I use conditional tags extensively on this website. How do you think I get the list of all previous tutorials I’ve written listed on only the tutorial pages? How is it the recent post listings display on the sidebar on every page, except the homepage? This is all through conditional tags. In this tutorial I’ll go over:

  • What exactly a conditional tag is
  • A list of the WP conditional tags and what their functions are
  • How to put a conditional tag to use while avoiding errors
  • Real examples of conditional tags used in WordPress themes

To learn everything you need to know about conditional tags and how it relates to WordPress themes, read on…

Warning: This tutorial has a lot of code and examples. Please press the back button if code makes you uneasy.

What are conditional tags and what can they be used for

Conditional tags are a very important part of your WordPress theme coding repertoire. They perform a similar role compared to the template hierarchy, which we went over briefly in the previous ultimate guide on the WordPress loop. Basically these quick snippets of PHP code can make your theme do something based on the type of WordPress page you’re on.

List of conditional tags and their functions

is_home() This is for use on the main blog page, usually your home page.
is_front_page() *WordPress 2.5 Only* If you’re using a static front page (not the standard latest blog posts) this is the conditional tag to be used.
is_single() To be used on single blog post pages.

is_single('1') Where 1 is the post ID.
is_single('hello-world') Where hello-world is the post slug
is_single('Hello World!') Where Hello World! is the post title

is_page() For WordPress pages.

is_page('2') Where 2 is the page ID.
is_page('about') Where about is the page slug
is_single('About Me') Where About Me is the page title

is_category() - When a category page is listed, similar arguments as the is_single() and is_page()
in_category('1') - When a post is in category ID 1.
is_tag() - On a tag archive page, similar to is_category().
is_archive() Any archive page including monthly, categories, and tags.
is_search() For search results pages.
is_404() When a page is not found

A complete list is provided on the WordPress Codex.

How to use WordPress conditional tags

If you know PHP, this part will be a breeze for you. If not, may be slightly more difficult - but I’ll be providing all the code and concepts you need so it won’t be too hard to understand. Let’s say you wanted to display a little welcome message for your visitors that’s only displayed on the home page.

For our example, we’ll use a freshly installed WordPress 2.5 with the default Kubrick theme. Let’s open up sidebar.php the theme editor. Hopefully your theme is writable before doing this.

Right after the sidebar div begins (<div id=”sidebar”>) we’ll place the following code:

<?php if (is_home()) { ?>
<p>Welcome blog visitor, perhaps you would like to subscribe to my feed? This message will only be displayed on the homepage.</p>
<?php } ?>

Just to clarify the code itself and it’s placement, here’s a screenshot of what it should look like in the theme editor:

WordPress Home Conditional Example

The conditional tag is used with an php if statement so therefore uses php tags. Notice the brackets, and how the php tag closes when there’s straight HTML displayed. Also notice how the PHP tag opens again to have a closing bracket. This is very important, as forgetting this will cause the entire page below to break with an error.

Now, let’s open up the test blog homepage in a web browser.

WordPress Home Conditional Example In Action

As you can see, that little message we placed in the sidebar is displayed in the sidebar on the homepage. It will not be visible on any other page of the site.

More examples of conditional tags in use

Okay, I know that was a pretty boring example, but hopefully you have the basic hang of it now.

Sidebar Manipulation

A lot of examples are actually present in the same sidebar.php file of the default theme. Let’s take a look at the following example in that:

<?php /* If this is the frontpage */ if ( is_home() || is_page() ) { ?>
<?php wp_list_bookmarks(); ?>
<li><h2>Meta</h2>
<ul>
<?php wp_register(); ?>
<li><?php wp_loginout(); ?></li>
<li><a href="http://validator.w3.org/check/referer" title="This page validates as XHTML 1.0 Transitional">Valid <abbr title="eXtensible HyperText Markup Language">XHTML</abbr></a></li>
<li><a href="http://gmpg.org/xfn/"><abbr title="XHTML Friends Network">XFN</abbr></a></li>
<li><a href="http://wordpress.org/" title="Powered by WordPress, state-of-the-art semantic personal publishing platform.">WordPress</a></li>
<?php wp_meta(); ?>
</ul>
</li>
<?php } ?>

This code will display your blogroll and meta items on the sidebar of the homepage and single pages only - not on any archive pages, author pages, 404 pages, etc. What those two lines together ( the || between the two conditionals) do is act as a logical OR function. Basically it’s saying - if the page you’re on is the homepage or a single page, display what’s inside the brackets.

How to get something to show up on all pages except

In the opening paragraph of this article, I mentioned how I get recent post articles to display on every page, except the homepage. Here’s a little code hack that will allow you to do this, taken straight from my sidebar.

<?php if (is_home()) { } else { ?>
<h2 id="recentposts">Recent Posts</h2>
<div class="cont">
<ul>
<?php wp_get_archives('title_li=&type=postbypost&limit=10'); ?>
</ul>
</div>
<?php } ?>

Basically this code is saying: if on the homepage, do nothing - else display the recent posts.

Why would I want to do this? Probably because you can already see the latest posts when you’re on the homepage, why have another list of the exact same thing? Please note you may not need that extra CSS formatting because like I said, this code was taken directly from my sidebar - my template.

Conditional Tags in other places

On the Pushing Borders theme, I used conditional tags to display a different heading corresponding to the type of page a user was on. If you download and look at the header.php file, you’ll see the following in the header div.

<big><?php if (is_home()) { echo "home"; } elseif (is_single()) { echo "blog"; } elseif (is_page()) { echo "page"; } elseif (is_search()) { echo "search"; } elseif (is_archive()) { echo "archive"; } ?></big>

Instead of making separate little if statements, I incorporated this into one big if elseif statement encompassing all the major conditional tags into one. Make sure you use proper syntax or you will run into errors. This Elseif tutorial from Tizag.com does a good job of explaining the PHP aspect of things.

Conclusion

Conditional tags can be a very powerful thing. Be creative with it. The possibilities certainly aren’t limited to the examples here. Check out what other blogs do with their sidebars and take note of changes on different types of pages to get ideas.

Hope you enjoyed the guide. Feel free to comment and share if you liked it. Subscribe to the feed to get all the latest theme release and tutorial updates.

Subscribe to RSS

By: Leland on Apr. 14

Bookmark and Share

32 Comments »

Comment by WordPress Modder
2008-04-15 09:57:45

Great post, very nice to see conditional tags broken down to the basics with “real world” examples. I will be directing my readers here to learn more…

 
Comment by Banago Subscribed to comments via email
2008-04-15 12:14:44

Hello bady!

Thanks for this great article. I really find it a must-read for every wordpresser.

 
Comment by Richard H
2008-04-21 16:53:11

Great guide Leland. I’m sure it’ll be very helpful to a whole host of skill levels in the WP community.

Many thanks.

 
Comment by legrosszabb Subscribed to comments via email
2008-05-08 12:52:34

It isn’t work:

Why? Why can’t I write more page ID after the if(is_page(’….

With one ID, it is work. But with more, it isn’t. Have you idea?

Comment by Leland
2008-05-19 15:47:29

This array method was just introduced in WordPress 2.5:

is_page(array(42,'about-me','About Me And Joe'))

More information here.

 
 
Comment by Brent
2008-05-19 15:42:43

How can i say not to show. So that if i have say a section in my sidebar i do not want to show on a page because it is repetative.

I want to say if_single(’about’) then dont show.

Comment by Leland
2008-05-19 15:45:47

You would put an exclamation point before it, like this (assuming you mean the about page):

<?php if (!is_page('about')) { ?> Stuff shown on everything but about page <?php } ?>

Hope this helps.

 
 
Comment by Justin Kent
2008-06-08 13:05:14

Nice work!

I’m trying to figure out the conditional tag for:
if this post is a member of category X

Any suggestions?

Thanks for the guide.

Comment by Leland
2008-06-09 06:54:29

@Justin Kent: That would be - in_category(’1′) - When a post is in category ID 1.

 
 
Comment by teddY
2008-06-12 08:22:46

Thank you so much for shedding light on Wordpress’ conditional tags! I’m not very good at php and so I can only use deductions to guess what the tags are saying. The Wordpress codex gives me a list of conditional tags, but it didn’t go into much depth about their application - and you’ve did it! The examples really explains everything :)

 
Comment by Leland
2008-06-12 10:51:19

@teddY: No problem, glad you found the tutorial useful. :D

 
Comment by kiviniar Subscribed to comments via email
2008-06-13 17:25:55

This explains a lot, just had a query as how I could use a different stylesheet for the single post page.

Basically I want to have a different layout on the single post but do not know how to use conditional tags.

Can help will be greatly appreciated..

Thanks

 
Comment by barry
2008-08-05 12:42:39

Thanks, I can understand how conditional tags work now, but I can’t make it do what I want.

I want a different sidebar to load only when a post from a specific category has been queried.

I used is_category(’5′) on the category.php template file which works fine.

Is there no conditional tag to make something load up only if it’s a post of a specific category on single.php ??

Thanks for help

 
Comment by Leland
2008-08-05 12:55:39

@barry: Did you try in_category()?

 
Comment by RH
2008-09-16 10:39:26

Interesting stuff, I may need to try it out sometime when I’m finished procrastinating working.

 
Comment by Sandra
2008-09-30 12:30:46

Thank you for your post explaining the conditional tags.

 
Comment by Mark McWilliams
2008-10-21 18:14:49

After asking Kyle over at WPHacks.com about this topic, he pointed me towards this post, and for that I’ll need to go and thank him. :D

I’ve read about these tags, and also asked a couple of people about them, but this has to be one of the best tutorials/guides that I’ve read on the topic!

You now have a new email subscriber, as I’ve just been and signed up. I’m looking forward to more posts, and keep up the good work.

Thanks
Mark

 
Comment by tsquez
2008-12-19 17:16:03

Your tutorials are awesome. I keep coming back whenever I need help with something and this is one of those times. This tutorial is simple and easy to follow. I was looking for something in regards to conditional tags for a site I am developing and this was the easiest one I could find. This helped me accomplish exactly what I was hoping to do. Thanks again not only for an amazing site but great content! Keep it up!!!

 
Comment by Max Subscribed to comments via email
2009-02-09 06:44:25

Hi! Thanks for this tutorial it helped solve an annoying problem of mine.

I’m just starting out in PHP and find Wordpress really useful to base the code in something ‘real’.

Could I ask you a question about something you said…


Also notice how the PHP tag opens again to have a closing bracket. This is very important, as forgetting this will cause the entire page below to break with an error.

As a newbie I’m struggling to find out how this code works (even though it does) - could you explain what is going on here? I’d have thought that by closing the initial php tag before the html code, parsing would just skip the html altogether - because the html is no longer contained within php tags.

I know it’s a dufus question but I’m just starting out :D

Thanks!

 
Comment by mo
2009-03-05 05:18:01

Hey - thanks for this. Really good guide. Had been looking for an all-encompassing post on conditional tags for a while.

 
Comment by Shanna
2009-03-09 02:43:26

Thanks so much - just what I was looking for to try & add a listing of post tags onto just the single page of an individual blog post (single), but not on the homepage, category, or archival listings of a number of posts. Your instructions were concise & easy-to-follow.

 
Name (required)
E-mail (required - never shown publicly)
URI
Subscribe to comments via email
Your Comment (smaller size | larger size)
You may use <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> in your comment.

Trackback responses to this post

Recent Comments

  • Anto: Hello. Thanks, you’ve got my contact details. Get in touch with me if your wanting something in your own...
  • Sonny: Hey Anto, I love this design, good work that I paid for link removal :) Anyways, how can one contact you to do...
  • Leland: @axelk: As in picture uploads? Not sure about that, although you could set up the form to allow users to send...
  • axelk: thank you for the reply. can Contact 7 be used to submit pictures as well?
  • Leland: @axelk: I think that’s what the Contact Form 7 is for. Although if you wanted to allow users to submit...

About The Author

Welcome to Theme Lab. My name is Leland, and I run the place. I write all the tutorials and code all the themes you see here on the site.

For more information about me, feel free to read the about page. If you want to drop me a message, feel free to contact me.

Interested in advertising on Theme Lab? We have advertising available at very reasonable rates. Head on over to the advertising page for further details.

Please read our Privacy Policy and Terms of Service in regards to theme usage.

© 2009 - Theme Lab - Powered by WordPress