ThemeLab's Blog

Stay up to date with our newest WordPress themes, WordPress plugins, WordPress tutorials, and other announcements.

The Ultimate Guide to WordPress Conditional Tags

Last Updated on by

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(); ?>
<?php wp_register(); ?>
<li><?php wp_loginout(); ?></li>
<li><a href="" title="This page validates as XHTML 1.0 Transitional">Valid <abbr title="eXtensible HyperText Markup Language">XHTML</abbr></a></li>
<li><a href=""><abbr title="XHTML Friends Network">XFN</abbr></a></li>
<li><a href="" title="Powered by WordPress, state-of-the-art semantic personal publishing platform.">WordPress</a></li>
<?php wp_meta(); ?>
<?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">
<?php wp_get_archives('title_li=&type=postbypost&limit=10'); ?>
<?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 does a good job of explaining the PHP aspect of things.


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.


Leland Fiegel was the original founder of ThemeLab. He is a web developer who loves WordPress and blogging.

  • Pingback: ultimate Guide to Wordpress [if] Tags « Rif.wp memo

  • WordPress Modder

    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…

  • Banago

    Hello bady!

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

  • Pingback: Five Articles a Week Can Make You a Geek (1) |Twenty First (Century) Blog

  • Pingback: Improve Your Sites Behavior With WordPress Conditional Tags

  • Richard H

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

    Many thanks.

  • legrosszabb

    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?

    • Leland

      This array method was just introduced in WordPress 2.5:

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

      More information here.

  • Pingback: WordPress Conditional Tags - Posted In WordPress | Bayu Albelly - .SlackbloG Journal

  • Pingback: WordPress Freelancer » Five Articles a Week Can Make You a Geek (1)

  • Brent

    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.

    • Leland

      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.

  • Pingback: Improve Your Sites Behavior With WordPress Conditional Tags | [Blog Tutorials]

  • Justin Kent

    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.

    • Leland

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

  • teddY

    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 :)

  • Leland

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

  • Pingback: teddY-risatioN™ » teddY-risatioN™ Zeta Released!

  • kiviniar

    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..


  • Pingback: Enhanced Sidebars with Dynamic Content | Vandelay Website Design

  • barry

    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

  • Leland

    @barry: Did you try in_category()?

  • RH

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

  • Sandra

    Thank you for your post explaining the conditional tags.

  • Mark McWilliams

    After asking Kyle over at 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.


  • Pingback: The Makings of a WordPress Design Blog | Rob Barrett Design

  • Pingback: WordPress Freelancer » Five Articles a Week Can Make You a Geek

  • tsquez

    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!!!

  • Max

    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


  • mo

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

  • Shanna

    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.

  • Pingback: Enhanced Sidebars with Dynamic Content « Online Free Application Software Tips Tools Wallpapers

  • Pingback: Jenn Mears Web Design » Site Launch: Michael McCarthy Law

  • Pingback: 5 Useful And Creative Ways To Use WordPress Widgets | SEO & Web Design

  • Pingback: 5 Useful And Creative Ways To Use WordPress Widgets | Kosvo Youth Portal

  • Pingback: 5 Useful And Creative Ways To Use WordPress Widgets

  • violet

    This was incredibly helpful! Took me forever to realize what I needed was a conditional tag, but once I did, this solved the problem. Thanks!

  • iamseo

    Wanted to let you know that this is possibly the page I look at most when coding WP themes :)

  • Brow

    Awesome post! I was stuck on getting posts to show on JUST ONE category and this helped big time.

    Thanks a million!

  • Skip Savage

    Great reference post you’ve written. Perhaps for the benefit of others with this same question, could you help me figure out how to conditionally tag both pages and a category in a single Widget Logic field?

    For example, I have `is_page(array(‘something’,’someone’))` and I need to add `is_category(‘somewhere’).

    But I am stumped.

    • Leland

      Try putting an OR or || as a separator. AND won’t work since a page cannot be assigned to a category.

      is_page(array(’something’,’someone’)) || is_category(’somewhere’)

      • Skip Savage

        Thanks for the help.

        Sorry to report that OR or || as separators didn’t work for me.

        For anyone else with this question, I found using && gave me the result I was looking for.

        For example,

        `is_page(‘one’) && is_category(‘two’)`

        • AnonyMouse

          Same here. Coding a thesis_hook_before_sidebar, only && worked.

  • Clare

    Thanks for documenting this – I was looking for a way to add some information to my footer that I only wanted to appear on posts not pages. Your code worked a treat!

  • Seth Shoultes

    If anyone wants to see another example of how to use Leland’s tutorial to fix a Jquery laden theme or a busted plugin.

    I posted a real life example of a Themeforest Jquery Fiasco in my forums.

    Hope you will find it as useful as I did.

  • http://Various! MikeD

    This was EASILY the most understandable guide I’ve come across in my search for using conditional tags in WordPress. Can’t thank you enough!

  • Jon Wade

    Hi, I am trying to add some code to the loop.php of my theme, so that the author of each post is displayed below the title. I have done this, but I do not want it to appear on the front page (I use a static page for the homepage). Is there a way to say “if not the front page”?

    Struggling! Many thanks.