Skip to main content
Easily create better & faster websites with the Total WordPress Theme Learn More
(opens a new tab)

Discover WordPress Conditional Tags

We all use conditional statements in our developments under WordPress, right? But do you know all built-in WordPress conditional tags? They are simply great because thanks to these tags it’s very easy to create conditional statements. You can for example display content only for some users, or load scripts only if a page is using a specific custom template, or even styling a post if this post is sticky.

Conditional tags can be used in your themes or in your plugins, it doesn’t matter, but note that in some plugins it will be sometimes a bit tricky to use some tags (in fact all themes specific tags).

Available conditional tags

Here is the list of available conditional functions:

You probably know some of them, but do you really know the difference between is_single_() and is_singular() ? Or between id_day() and is_date() ? Did you know is_preview() ? To understand how each function works, simply click on the tag name, and read carefully the instructions in the codex.

All conditional tags return TRUE or FALSE

Conditional tags are one of my favorite WordPress built-in feature and i really encourage you to use them widely in your codes. All functions always return TRUE or FALSE, never IDs or array().

A close relation with templates hierarchy

As you might know, WordPress uses a really powerful template hierarchy system. Well, the use of conditional tags are closely related to this hierarchy. This is the principle used to select the template page to use depending on the URL you are visiting.

Conditional tags accept many arguments

When using these conditional tags you have to know that you can pass them different variables such as IDs, slug, or arrays. Here is is an example taken from the codex with the is_author() function:

is_author()
When any Author page is being displayed.
is_author( '4' )
When the archive page for Author number (ID) 4 is being displayed.
is_author( 'Vivian' )
When the archive page for the Author with Nickname “Vivian” is being displayed.
is_author( 'john-jones' ) 
When the archive page for the Author with Nicename “john-jones” is being displayed.
is_author( array( 4, 'john-jones', 'Vivian' ) ) 
When the archive page for the author is either user ID 4, or user_nicename “john-jones”, or nickname “Vivian”.

Examples

This is how nearly all conditional functions work but you can of course combine many conditions:

if ( is_home() && in_category( array( 9, 'blue-cheese', 'Stinky Cheeses' ) ) ) {
   the_content();
}
else {
   the_excerpt();
}

or you can do an action if a situation or another occurs:

if ( is_home() || in_category( array( 9, 'blue-cheese', 'Stinky Cheeses' ) ) ) {
   the_content();
}
else {
   the_excerpt();
}

And if you want to do a negative condition, use ” ! “:

$paged = $wp_query->get( 'page' );

if ( ! $paged || $paged < 2 ) 
{
    // This is not a paginated page (or it's simply the first page of a paginated page/post)
} 
else 
{
   // This is a paginated page.
}

And finally, using conditional tags is like any other “else if” PHP statement:

if ( is_page( 'about' ) || '2' == $post->post_parent ) {    
    // the page is "About", or the parent of the page is "About"
    $bannerimg = 'about.jpg';

} elseif ( is_page( 'learning' ) || '56' == $post->post_parent ) {	
    $bannerimg = 'teaching.jpg';

} elseif ( is_page( 'admissions' ) || '15' == $post->post_parent ) { 
    $bannerimg = 'admissions.jpg';

} else { 
    $bannerimg = 'home.jpg'; // just in case we are at an unclassified page, perhaps the home page
}	

Creating custom conditional tags

In some cases you need to create your custom conditional functions. That’s pretty easy to do, but very often functions names aren’t following the same logic as the one used by core developers. Please use the same naming convention using prefix such as “is_“, “has_“, “in_“. This is very helpful in particular if you’re not the only person to work on a specific script.

Recent in WordPress Tutorials
Tutorials

How to Add Code Snippets in WordPress

Learn how to easily add code snippets to WordPress. We explain what code snippets are, why use them and how to add snippets in WordPress.
11 Comments
  1. AJ Clarke | WPExplorer · 12 years ago

    Conditional tags are crucial in any theme…I don’t think there is a single WordPress product where I haven’t used them. One of the first things any person learning WordPress should know.

    Thanks for sharing Remi!

  2. Rooteto · 12 years ago

    Beautiful description.

    Thanks 🙂

    • Remi · 12 years ago

      Thank you!

  3. amdhas · 12 years ago

    What differentiate the condition if, else, if else and switch, break?

    Which is better at sorting on condition?

    • Remi · 12 years ago

      Hi, if/else or if/elseif/else and switch are doing pretty much the same thing but using “switch” is better when having to test multiple potential values.

  4. Reind · 12 years ago

    Awesome list. Something you allways need and also allways seem to forget when you need them. So BOOKMARKED 😉

    • Remi · 12 years ago

      Yeah that’s true, we’re always trying to remember some of them, so having them in a list is really nice. Note that none all of them are listed here, for example is_user_logged_in() or wp_script_is()… but i listed the most used.

  5. Frank · 12 years ago

    A small hint to learn and understand conditional tags. use the plugin Debug Objects in WordPress and see the output of tag Conditional tag of each page. It is easy to learn the tags and his function with this plugin.

  6. bucur · 12 years ago

    I like nice stuff…

  7. Mannu Uppi · 10 years ago

    Hi sir, I want to insert adsense ads in the first post of index page only. How can I do so with help of conditional tags ??

    Thanks in advance.

    • AJ Clarke · 10 years ago

      You can’t do so with conditional tags, you’ll have to add a counter variable in your loop.

Sorry, comments are now closed.