The conditional tags ( conditional tags ) are the functions of WordPress that returns a Boolean value (true or false) depending on the result of verification. This verification can be performed on each component or section of WordPress. This article will present the most used conditional tags.
is_home ()
In the case of is_home()the condition is true if you are viewing the home page of the site. If the main page is a static page, use instead is_front_page()that will be analyzed below.
is_front_page ()
With is_front_page()the condition is true if you are viewing the home page of the site and if the home page is a static page. In practice we can combine the two functions proposed so far in order to always select the homepage:
if (is_home () || is_front_page ()) {...}
is_admin ()
In is_admin()the condition it is true when the administrative section of WordPress is displayed, ie /wp-admin.
is_single ()
In the case of is_single()the condition is true when viewing a single post. Some examples of what just described could be the following:
is_single (17)
true when the post is ID 17.
is_single( 'Test' )
true when the post is titled “Test”.
is_single( 'test-slug' )
true when the post has slug (abbreviation) “test-slug”.
is_single( array( 17, 'test-slug', 'Test' ) )
true when the post is ID 17 or slug “test-slug” or as a “Test” title.
is_single( array( 17, 19, 1, 11 ) )
true when the post has ID as one of the values indicated in the array.
is_single( array( 'test-slug', 'slug', 'slug-2' ) )
true when the post has slug one of the values indicated in the array.
is_single( array( 'Test', 'Test 2', 'Test 3' ) )
true when the post is titled one of the values indicated in the array.
is_sticky ()
With is_sticky()the condition it’s true when a post has been marked as “in evidence” in the Loop . This function is typically used in the Loop and in that case the default argument is the ID of the current post. If, on the other hand, it is used outside the Loop, it is always necessary to specify the ID of the post on which to carry out the verification as an argument.
is_page ()
In is_page()the condition it is true when viewing a page. The syntax and examples for this function are the same as for the function is_single(); be careful, however, there is still a function in WordPress to determine if a page is descendant (daughter) of another, ie if it is a subpage. To remedy this lack the solution is as follows:
if( is_page() && $post->post_parent > 0 ) { ... }
is_category ()
Relative to is_category(), the condition is true when viewing a category. The syntax and examples for this function are the same as for the function is_single().
in_category ()
Regarding in_category(), in his case the condition is true when a post belongs to a certain category, for example:
in_category( 5 )
true if the post belongs to the category with ID equal to 5.
in_category( array( 2, 3, 4 ) )
true if the post belongs to the categories with ID equal to the values specified in the array.
is_tag ()
In is_tag()the condition it is true when viewing the archive of a given tag, as in the following examples:
is_tag( 'test' )
true if the tag has a “test” slug.
is_tag( array( 'test', 'test-2', 'test-3' ) )
true if the tag has one of the values specified in the array as slug.
has_tag ()
has_tag() verify that a post has a specific tag.
has_tag( 'test' )
true if the post has “test” as a tag.
has_tag( array( 'test', 'test-2', 'test-3' ) )
true if the post is tagged as one of the values specified in the array.
is_tax ()
With is_tax()yes you are true if you are viewing the archive page of a taxonomy, for example:
is_tax( 'test' )
true if the taxonomy is called “test”.
is_tax( 'test', 'slug' );
true if the taxonomy test has slug “slug”.
is_tax( 'test', array( 'test-1', 'test-2', 'test-3' ) )
true if the “test” taxonomy slugs one of the values specified in the array.
has_term ()
This tag verifies that a post has certain terms related to its taxonomies. The second parameter must be the slug or the name of the reference taxonomy; for which:
has_term( 'test', 'tassonomia' )
true if the post has the term “test” for the taxonomy “taxonomy”.
has_term( array( 'foo', 'bar', 'baz' ), 'tassonomia' )
true if the post has one of the terms specified in the “taxonomy” taxonomy array.
is_author ()
In the case of is_tax()true, when the archive page of an author’s posts is displayed; in this regard, observe the following examples:
is_author (4)
true when the author has user ID 4.
is_author ('Mario')
true when the author’s nickname is “Mario”.
is_author ('mario-rossi')
true when the author’s Nicename is “mario-rossi” (the Nicename of the author appears as the last part of the author page URL).
is_author (array (4, 'mario-rossi', 'Mario'))
true when the author has 4 as user ID or has Nicename as “mario-rossi” or as nickname “Mario”.
is_archive ()
With is_archive()it is true when we find ourselves in an archive page (category, tag, taxonomy, author etc.).
is_search ()
With this conditional tag we have true when we are viewing the search results page.
is_404 ()
true when the website generated a 404 error page (handled by the file 404.phpin the themes).
is_paged ()
true when a section presents post pagination created after a Loop.
is_main_query ()
In the case of is_main_query()true is returned when the loop created by a query is the main loop of the site. For main loop and main query basically means that which includes all the posts of the site. A typical use of this tag is to create filters on posts:
function exclude_cat( $query ) { if( !is_admin() && $query->is_main_query() && ! $query->get( 'cat' ) ) { $query->set( 'cat', '-9' ); } } add_filter( 'pre_get_posts', 'exclude_cat' );
In this case, we removed the category with ID 5 from the main loop.
has_excerpt ()
With has_excerpt()it is true when a post has a summary ( excerpt ) set in the editor. The function uses the ID of the current post in the Loop, while outside the Loop accepts the ID of the post to be tested.
has_post_thumbnail ($ id)
In has_post_thumbnail( $id )it is true if the post whose ID is specified as a parameter has a highlighted image set.
Using conditional tags outside the Loop
// functions.php function add_social_media( $content ) { $social = social_plugin(); if( is_page() ) { // qualsiasi pagina $content = $social . $content; } if( is_single() ) { // qualsiasi post singolo $content = $content . $social; } return $content; } add_filter( 'the_content', 'add_social_media' );
In this case we have done two different operations depending on the position of the content (in a page or in a post). If we need more specificity we can use the parameters of these functions:
// functions.php function add_scripts() { wp_enqueue_script( 'jquery' ); if( is_page( 8 ) ) { // pagina specifica con ID 8 wp_register_script( 'google-maps', get_template_directory_uri() . '/js/gmap.js', array( 'jquery' ), '2.0', false ); wp_enqueue_script( 'google-maps' ); } //... } add_action( 'wp_enqueue_scripts', 'add_scripts' );
Use conditional tags in the Loop
<!-- index.php --> <?php while( have_posts() ) { the_post(); $id = get_the_ID(); ?> <figure class="featured"> <?php if( has_post_thumbnail( $id ) ) { the_post_thumbnail(); } else { ?> <img src="http://placehold.it/400x300" alt="" /> <?php } ?> </figure> <?php } ?>
In this case, if the posts do not have a highlighted image, we will set a default one.
Conclusion
Conditional tags are a very useful and fundamental tool in WordPress. Without them, most of the operations performed by themes and plugins would not be possible.