How to Use Wordpress Conditional Statements

Are you new here?

If this is your first time visiting our website, welcome! Learn more about us and what we do, or look through our complete blog archives for more good ideas.

Blog Posts from Addicott Web About Wordpress

When I’m creating a Wordpress website, often times I’ll want a certain image, or a certain snippet of text, to appear on a particular page, but not on any others. How do you do that without creating multiple page or post templates? It’s quite simple actually: use conditional statements.

(Word of warning: what I’m about to talk is a more advanced topic intended for those who have an interest in building, or working with, Wordpresss theme files. I’m assuming that you have at least a working knowledge of HTML, CSS, and PHP in order to understand what I’m talking about.)

What are conditional statements?

Conditional statements are simply some logic that tells the Wordpress database what content is displayed (and how that content is displayed) based on what conditions that page matches.

At their most basic level, conditional statements look like this:

<?php if ( ) { ?>
<?php } else ( ) { ?>
<?php } ?>

Basically what this says is that there is a certain condition, and if that condition is met, then display whatever HTML, CSS, PHP, etc. is specified there; if that condition isn’t met, do this instead. So far, so good, right?

What conditional tags can I use?

Within a conditional statement you’ll find what are known as conditional tags. Basically, these little tags call a particular piece of information from the Wordpress database. There are conditional tags for most things in Wordpress – pages, posts, categories, tags, etc.

The Wordpress codex goes into more detail about all of the conditional tags that are available, but some of the most basic and popular ones are:

  • is_page()
  • is_single()
  • is_category()
  • is_author()
  • is_home()

Using them is pretty simple – you can identify the page/post/category/author by ID number, slug (name), or title. This might make more sense with an example.

Say you want to apply a specific condition to your “About” page. That page has the database ID number “3″, the title is “About Us”, and the slug (name) is “about-us”. You could use any of those in the is_page() condition as follows:

  • is_page(‘3′)
  • is_page(‘About Us’)
  • is_page(‘about-us’)

The same applies to all of the other different uses. Say you want some particular links to appear in the sidebar when someone is on a particular category page – let’s say the category is “Apples”, and it has a database ID number “5″. You could use the is_category() condition like this:

  • is_category(‘5′)
  • is_category(‘Apples’)
  • is_category(‘apples’)

More complicated uses of conditional statements

Now that you know what a basic conditional statement looks like and what conditional tags are available to use, we can really have some fun incorporating them into your Wordpress theme.

Head’s up: this is where it will start to get a bit more complicated. But let’s go through a few examples for you to get an idea of the different ways that conditional statements can be used, and hopefully you’ll pick up the gist of how to use them.

Example #1: Multiple conditions

Let’s say that you have 3 pages on your Wordpress website. You want to display an image on one page, some text on another, and nothing on the third.. What would this look like?

<?php if ( is_page('Apples') ) { ?>

    <img src="apples.gif" />

<?php } elseif ( is_page('Oranges') ) { ?>

    <p>This is my text.</p>

<?php } else { ?>

<?php } ?>

This is what I call a multi-conditional statement. Basically this says, “if the page is the page about apples, then display the picture of the apples; if the page is the page about oranges, then display that text; and if it’s neither, then don’t display anything”.

One note about multi-conditional statements: you can have as many different conditions as you want. If you have 20 different categories and want to display 20 different things in the sidebar of each, you can do that – there’s no limit.

Example #2: This OR That

Let’s say you want to display a particular piece of text at the top of a single post OR for a particular category page. What would your conditional statement look like?

<?php if ( is_single('Apples') || is_category('Vegetables') ) { ?>

    <p>This is my text.</p>

<?php } else { ?>

<?php } ?>

The “||” basically tells Wordpress to display that content if the the page/post/etc. meets either the first condition OR the second condition. If it doesn’t meet either, then it won’t display anything.

Example #3: Apply the same condition to many things

Let’s say you want to display some text at the top of both the page about apples AND the page about oranges, while not displaying that text at the top of any other pages. What would your conditional statement look like?

<?php if ( is_page(array('Apples','Oranges')) ) { ?>

    <p>This is my text.</p>

<?php } else { ?>

<?php } ?>

This will display the exact same thing as in example #1, and you’re probably asking yourself, what’s wrong with writing it the long way, like I had in that example.

Technically speaking, there isn’t anything wrong with doing it like that. It’s just that combining your conditions is the simpler way of doing things and easier to maintain. Say you want to update the text that’s displayed at the top of both pages. Would you rather update it once, or have to do it multiple times? I’d rather do it just once.

Example #4: Using variables

Variables are another way of calling information from the Wordpress database. Often used in plugins and in your Wordpress loop, you can also use them within conditional statements.

Let’s say that you want to display in the sidebar a list of links to the sub-pages of a particular parent page, and you want that list to also appear on the parent page as well. What would your conditional statement look like?

<?php if ( is_page('Fruits') && $post->post_parent=="Fruits" ) { ?>

    <ul>
    <li>List item</li>
    </ul>

<?php } else { ?>

<?php } ?>

In this case, we’re using the $post->post_parent variable to say that we’re applying the condition to all the sub-pages of the “Fruits” page, as well as the actual parent page itself, which we’ve identified with the other condition.

Example #5: “Is Not” Conditions

Continuing with our use of variables, let’s say that you want to display an image at the top of all pages that have the “Fruits” page as their parent page EXCEPT FOR the page about bananas. What would this look like?

<?php if ( $post->post_parent=="Fruits" && !( is_page('Bananas') ) ) { ?>

    <img src="fruits.gif" />

<?php } else { ?>

<?php } ?>

Here, we’re using the “&& !” to say AND EXCLUDE the page about bananas from the condition that we’re applying to every other sub-page.

Thoughts?

Using conditional statements within your Wordpress theme files is a great way to help cut down on the amount of code that you’re writing when you create a Wordpress website. This makes it easier to maintain in the long run, and is a great way to take advantage of the database-driven nature of Wordpress.

Do you use conditional statements on your Wordpress website, or when you’re creating a Wordpress theme for a client? If so, share your thoughts about them with everyone by leaving a comment below!

Similar Posts

14 Comments

  1. Joel wrote on October 22, 2009:

    I believe examples 3 and 4 above won’t work as explained. You need to replace the “&&” with “||”.

    I’m no expert, but you can be fairly certain that a Wordpress page will never have two names at the same time, so example 3 (part 1 of 2) will never return TRUE; although, part 2 of 2 in example 3 does seem like it would work as explained.

    Lastly, as written, example 4 seems like it would only return TRUE for a page named “Fruits”.

    Hope that helps.

    Posted at 9:42 pm

    • Joel wrote on October 22, 2009:

      Actually, as written, I don’t think example 4 will ever return TRUE either.

      Posted at 9:47 pm

    • Addicott Web wrote on October 22, 2009:

      Thanks for your comments Joel! What I was trying to show in the first part of example 3 is that you can apply the same thing to two different pages. You’re right though – now that I look at it again, I don’t think it would come out like that – you would need to use the “||” instead.

      Example 4 will work actually – I’ve used that one successfully many times, which is where I pulled it from. I usually use the ID number rather than the title, which I was only using for illustrative purposes, but either way, it will work to display the list on both the parent page and on all of the sub-pages of that parent page.

      Posted at 10:19 pm

  2. arena wrote on October 23, 2009:

    can a page be Apples AND Oranges at the same time ?

    Posted at 8:07 am

    • Addicott Web wrote on October 23, 2009:

      What I was trying to show is that you can apply the same thing to two different pages. On second glance though, the way I wrote it wouldn’t do that – I would need the “||” instead. I’ll update it so now one else gets the wrong impression about it!

      Posted at 8:23 am

  3. Matt wrote on October 23, 2009:

    Thanks for the first example – I couldn’t remember how to do the else statement so I had been using multiple single conditional statements for each are (page, archive, single, home, etc.) Allowed me to clean up about 15+ lines of code into 5.

    Posted at 11:08 am

  4. Kim Doyal wrote on October 23, 2009:

    Thanks for this!
    I’ve been working with WordPress for a while and am now digging deeper into the code side of things (who knew it was kind of fun? :-) ).
    Tweeted & stumbled this….
    Thanks,
    Kim Doyal
    The WordPress Chick

    Posted at 1:15 pm

  5. Tony wrote on October 28, 2009:

    Very useful article for someone just trying to delve into PHP.

    I just need to find a GOOD way to translate IP Address into Country Code, and then I plan to use this to display country specific information to visitors, in particular advert blocks.

    Posted at 8:06 am

  6. Ares wrote on October 29, 2009:

    In example #5 you don’t actually need the parenthesis for the negation. You are just adding “more” work to the parser.

    So it could be rewritten as follows:

    post_parent==”Fruits” && !is_page(‘Bananas’) ) { ?>…

    Posted at 2:30 am

  7. Code Moi Ca wrote on January 18, 2010:

    Oh this article is great it sent me on the right track. All I wanted to do was display an ad on single posts and in my archive page! Cheers.

    Posted at 1:48 pm

Trackbacks/Pingbacks

  1. [...] Web has a great little article online that goes in depth on the uses of WordPress conditional statements. The Conditional Tags can [...]

  2. How to Use Conditional Statements…

    When I’m creating a Wordpress website, often times I’ll want a certain image, or a certain snippet of text, to appear on a particular page, but not on any others. How do you do that without creating multiple page or post templates? It’s quite simple ac…

  3. [...] How to Use Wordpress Conditional Statements [...]

  4. [...] How to Use Wordpress Conditional Statements – How To’s Article by Addicott Web, Chicago … – [...]

Leave a Comment