- Send us a note
- info@addicottweb.com
- (773) 633-7078
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.
Posted on October 22, 2009 under How To's, 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
- Create Single Post Pages for Different Categories (July 14, 2009)
- SEO for WordPress: The META Tag Problem (January 30, 2009)
- Ways to Create Breadcrumbs in WordPress (April 29, 2009)
16 Comments
Trackbacks/Pingbacks
-
[...] Web has a great little article online that goes in depth on the uses of WordPress conditional statements. The Conditional Tags can [...]
-
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…
-
[...] How to Use WordPress Conditional Statements [...]
-
[...] How to Use WordPress Conditional Statements – How To’s Article by Addicott Web, Chicago … – [...]

Warning: call_user_func(mytheme_comment) [function.call-user-func]: First argument is expected to be a valid callback in /hermes/waloraweb000/b1918/addicottweb/wp-includes/comment-template.php on line 1334
Warning: call_user_func(mytheme_comment) [function.call-user-func]: First argument is expected to be a valid callback in /hermes/waloraweb000/b1918/addicottweb/wp-includes/comment-template.php on line 1334
Warning: call_user_func(mytheme_comment) [function.call-user-func]: First argument is expected to be a valid callback in /hermes/waloraweb000/b1918/addicottweb/wp-includes/comment-template.php on line 1334
Warning: call_user_func(mytheme_comment) [function.call-user-func]: First argument is expected to be a valid callback in /hermes/waloraweb000/b1918/addicottweb/wp-includes/comment-template.php on line 1334
Warning: call_user_func(mytheme_comment) [function.call-user-func]: First argument is expected to be a valid callback in /hermes/waloraweb000/b1918/addicottweb/wp-includes/comment-template.php on line 1334
Warning: call_user_func(mytheme_comment) [function.call-user-func]: First argument is expected to be a valid callback in /hermes/waloraweb000/b1918/addicottweb/wp-includes/comment-template.php on line 1334
Warning: call_user_func(mytheme_comment) [function.call-user-func]: First argument is expected to be a valid callback in /hermes/waloraweb000/b1918/addicottweb/wp-includes/comment-template.php on line 1334
Warning: call_user_func(mytheme_comment) [function.call-user-func]: First argument is expected to be a valid callback in /hermes/waloraweb000/b1918/addicottweb/wp-includes/comment-template.php on line 1334
Warning: call_user_func(mytheme_comment) [function.call-user-func]: First argument is expected to be a valid callback in /hermes/waloraweb000/b1918/addicottweb/wp-includes/comment-template.php on line 1334
Warning: call_user_func(mytheme_comment) [function.call-user-func]: First argument is expected to be a valid callback in /hermes/waloraweb000/b1918/addicottweb/wp-includes/comment-template.php on line 1334
Warning: call_user_func(mytheme_comment) [function.call-user-func]: First argument is expected to be a valid callback in /hermes/waloraweb000/b1918/addicottweb/wp-includes/comment-template.php on line 1334
Warning: call_user_func(mytheme_comment) [function.call-user-func]: First argument is expected to be a valid callback in /hermes/waloraweb000/b1918/addicottweb/wp-includes/comment-template.php on line 1334