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).
or you can do an action if a situation or another occurs:
And if you want to do a negative condition, use ” ! “:
And finally, using conditional tags is like any other “else if” PHP statement:
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:- comments_open
- has_tag
- has_term
- in_category
- is_404
- is_admin
- is_archive
- is_attachment
- is_author
- is_category
- is_child_theme
- is_comments_popup
- is_date
- is_day
- is_feed
- is_front_page
- is_home
- is_month
- is_multi_author
- is_multisite
- is_main_site
- is_page
- is_page_template
- is_paged
- is_preview
- is_rtl
- is_search
- is_single
- is_singular
- is_sticky
- is_super_admin
- is_tag
- is_tax
- is_time
- is_trackback
- is_year
- pings_open
- post_type_exists
- is_post_type_hierarchical
- is_post_type_archive
- is_comments_popup
- taxonomy_exists
- is_new_day
- has_excerpt
- has_nav_menu
- in_the_loop
- is_active_sidebar
- is_main_site
- is_plugin_active
- is_child_theme
- current_theme_supports
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();
}
if ( is_home() || in_category( array( 9, 'blue-cheese', 'Stinky Cheeses' ) ) ) {
the_content();
}
else {
the_excerpt();
}
$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.
}
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
}
0 comments:
Post a Comment