You are here:Home » WordPress » List Of All WordPress Conditional Tags

List Of All 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.

0 comments:

Post a Comment