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()) {

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()) {
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; ?>

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; ?>

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.


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

Kyle Eslick is WordPress enthusiast who took his passion for WordPress to the next level in 2007 by launching as a place to share hacks, tutorials, etc. Follow Kyle on Twitter @KyleEslick!

  1. Kaspars says:

    For controlling widgets one could also use Widget Context or Widget Logic plugins, which don’t require fiddling with code.

  2. Youngistaan says:

    Nice post @Jhon. thanks i will try to use this.

  3. Micheal says:

    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

  4. w3sl3ymill3r says:

    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.

  5. rex says:

    Excellent wordpress hack. Best of all, the examples!!!

  6. karmin says:

    thanks, this is very useful, but i little confused with it..
    but, i will try for this…

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

  8. Richard says:

    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!

  9. Adrian - WPDONE says:

    Very useful tips. Got to try some of them.

  10. Pedro B. says:

    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.

  11. Joe Perez says:

    Awesome post! Expertly written – great show of examples John. Bookmarked, Stumbled and Dugg!

  12. billn says:

    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!

  13. Great tips, they will definitely come in handy. =)

  14. takien says:

    thanks, what a nice trick

  15. Karen says:

    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!

  16. Bookmark TH says:

    Very informative and well explained, I will be trying out the Conditional Tags to Change Styles on different pages.

    Thank you so much…

  17. binbin says:

    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 …

  18. Kahil says:

    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…

  19. k.roussi says:

    This was what I needed. I didn’t have to search more than 2 links to find everything all on your site! Thanks

  20. Shawn says:

    Will be modifying a plugin to act differently on category pages on the sidebar.. hopefully the is_category() will help.. thanks

  21. Dana says:

    Great article John… but there is a code error that I think may be throwing people off. In the early examples where you use OR to add multiple conditions, the closing parentheses, on the right, was missing (just before the curly bracket and ?) so the code didn’t work. it’s an easy thing to miss!. It should look like this :

    if (is_home() || is_category() || is_page()) { ?>

  22. Russ Bryan says:

    I have a featured content gallery that i only want to show up on my static home page. My theme has these templates:
    404 Template (404.php)
    Comments (comments.php)
    Footer (footer.php)
    Header (header.php)
    Main Index Template (index.php)
    Page Template (page.php)
    Sidebar (sidebar.php)
    Single Post (single.php)
    Theme Functions (functions.php)

    the code for the featured content gallery is this:

    How would i use conditional tag to make it only show up on the static home page, and one which template file would it go.


  23. Jim says:

    This article has been a great help to this wp newbie!

  24. Great tips, they will definitely come in handy

  25. Oris WilliamsF1 says:

    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.

  26. Nicole says:

    Thanks! This is a great post. I would like to add about 30 conditional tags so that a difference sentence with 6 different links will appear on each different tag (i.e. tagged post will have a different sentence and set of links). Will this slow down my site?

  27. danie says:

    How would you apply custom css for lets say page 4 on the pages that has pagination on them?

  28. Hello there,

    I’m trying to add texts on all posts under a specific tag but I can’t get it into work.
    Here’s my code snippet derived from your post:


    Based from the code above, I want to add the work “greek” on all posts under the “Greek Mythology” tag. Any idea how to fix it? Thanks.

  29. Dee says:


    In a single post view… how does one list only those tags that are connected to the post in the sidebar?

    I don’t want to clutter the sidebar with popular/all available tags.

    Have looked for plugins for this purpose, but have only come across those that list ‘related posts’ not tags.

    Any suggestions on how to achieve this?

  30. martine george w. says:

    Hi, your post is very well explained, only I am a complete newbie and would like to change my blog header and make it different from my website header. I tried with using the code you provided, replacing front page by blog page, but am not sure if I put it in the correct place. Theme – Edit Css, is that correct? Or do I need to put the code in the blog page, or both? Should it be immediately changed in the preview?

    Many thanks

Trackbacks/Pingbacks »

  1. WordPress Weekend Resources - May 15, 2009 | says:
  2. Blog-Her » Bookmarks for May 9th through May 19th says:
  3. WordPress Weekend Resources - May 15, 2009 | says:
  4. How To: Use WordPress Conditional Tags to Hack Your Theme - Blog Tutorial says:
  5. How To: Hack WordPress Theme Template Pages | The Topic : Exposed says:
  6. PSD to WordPress Child Theme – Body Conceptions | WordPress customization • Website design • Loyalty cards • Promotional materials says: