Conditional Tags in WordPress

In this article, I will try to explain in plain language what "Conditional Tags in WordPress" are. You can see the full list of conditional tags on this page.

Conditional tags are so named because they, surprise-surprise, check whether a certain condition is met. In reality, conditional tags are auxiliary PHP functions in WordPress. They are called "tags" to distinguish them from other PHP functions.

Most conditional tags are created for the convenient execution of checks for various conditions, such as whether we are on the homepage of the site, whether the user is logged in, and so on.

Characteristics of conditional tags in WordPress:

  • Each of the tags is created to check a specific condition. Roughly speaking, any function starting with is_ can be called a conditional tag.

  • Some conditional tags can take parameters. For example, is_page( 20 ) - checks the ID of the current page. If the ID is 20, then the condition is met (the tag triggers).

  • Any conditional tag returns a logical value: true or false. Based on this answer, you can do something or not.

Conditional tags are closely related to the theme files hierarchy.

IMPORTANT: you cannot use tags before the parse_query hook is triggered, which means you should start using them from the wp hook, because before this hook, there is no data for the conditional tags to work correctly (global variables that conditional tags use are not filled).

Thus, the conditional tag will not work if you use it in the theme file body functions.php, because this file is included before the wp hook is triggered. Usually conditional tags in the functions.php file are used inside other functions or hooks.

Example of using Conditional Tags

Let's say we need to display a link in the footer and we only need to do this on the homepage of the site. For these purposes, in WordPress, there is the conditional tag is_front_page(), or its equivalent is_home(). These functions return true, only if the visitor is on the homepage. In the code, it looks like this:

<?php
if( is_front_page() ){
	echo '<a href="http://example.com">Link text</a>';
}
?>

Instead of HTML, there can be PHP code that will be executed only on the homepage. Instead of is_front_page(), it can be is_home(). These are similar conditional tags, see the difference below.

List of WordPress Conditional Tags

The most complete list of conditional tags can be seen at this link.

is_home() (main page)
Returns true if this is the main blog page. By main, it is meant the content set for the main page, i.e. if in the settings (settings > reading) "Front page displays" is set to "A static page", then this tag will work only for the static page set as the front page, and not for the main page.
is_front_page() (front page)
Returns true if you are on the front page, regardless of the settings. Pagination pages (/page/2 and so on) are not considered.
is_single() (single post page)

The function takes parameters: Post ID, Post title, Post slug, or an array of any of these parameters.

if( is_single() ){ ... }     // will work for any post
if( is_single(10) ){ ... }   // will work if it is a post whose ID is 10
if( is_single('Привет мир') ){ ... } // will work if it's a post titled "hello world."
if( is_single('privet_mir') ){ ... } // will work if it's a post with the tag "privet_mir".
// Вариант с массивом
if( is_single( array('10','Привет мир','new_post') ) ){ ... }
// will work if it is a post with an ID of 10, or the title "Hello world", or the slug "new_post".
is_page() (page)
Page of the "page" post type. The function takes the same parameters as is_single(): Page ID, Page title, Page slug, or an array of any of these parameters.
is_page_template() (page template)

If this is a template page for a static page. The function can take a parameter of the template file name.

if ( is_page_template() ){ ... } // will work on a permanent page for which a separate template is used
if ( is_page_template('my_page.php') ){ ... } // will work on a permanent page, for which a separate template with the file name my_page.php is used
is_singular() (any singular page)
Any singular page, including posts, pages, and custom post types. This is a shorthand for this check: if( is_page() || is_single() ).
is_year() (yearly archive)
Yearly archive (example.com/2010)
is_month() (monthly archive)
Monthly archive (example.com/2010/06)
is_day() (daily archive)
Daily archive (example.com/2010/06/02)
is_time() (time based archive)
Time based archive.
is_date() (any date archive page)

Date archive page (general conditional tag). This is a shorthand for this check:

if( is_year() or is_month() or is_day() or s_time() )
is_author() (author page)

Author display pages. The function can take parameters: Author ID, Author name, and Author nicename. You can also pass multiple parameters in an array. Example:

if ( is_author() ) { ... }     // will work for any of the author's pages
if ( is_author(10) ) { ... }   // will work if the author's page with ID equal to 10 is displayed
if ( is_author('Виктор') ) { ... } // will work if the author's page with the name "Victor" is displayed
if ( is_author('Viktor') ) { ... } // will work if the page of the author with the nickname "Viktor" is displayed
// Вариант с массивом
if ( is_author( array('10','Виктор','Viktor') ) ) { ... }
// will work if the page of the author with ID 10, or Name "Viktor", or Nickname "Viktor" is displayed.
is_archive() (any archive page)
If it's any type of archive page: categories, tags, dates, authors, taxonomies (in WP 3.0). General conditional tag.
is_post_type_archive() (post type archive page)
If it's a custom post type archive page. For example, if we have a post type "knigi" and when registering it we indicated that it has an archive page, then its URL will be "example.com/knigi". This conditional tag will trigger on such a page.
is_paged() (paginated pages)
If it's a paginated page (/page/2 or /page/3 and so on).
is_category() (category page)
If it's a category page.
cat_is_ancestor_of() (is one category a child of another)
Checks if one category is a child of another specified category (checks all levels of the category tree).
is_tag() (tag page)

If it's a tag page. The function can take parameters: slug (name) of the tag or an array of tag names.

if ( is_tag() ) { ... }              // will work for any tags/tag pages
if ( is_tag('wordpress') ) { ... }   // will work for a label/tag page Slug (title) which is equal to 'wordpress'
// array variant
if ( is_tag( array('wordpress','tag_name' ) ) { ... }
// will work for a tag/tag page Slug (title) which is equal to wordpress or tag_name
is_tax() (any taxonomy page)

If this is a page of a taxonomy (typically category or tag pages). It can take two parameters: taxonomy name and the name of the current taxonomy element.

if ( is_tax('category') ) { ... }                   // will work if the category taxonomy is present in the request of the page being opened. Same as is_category()
if ( is_tax('category','category_slug') ) { ... }   // will work if it is a category page whose slug (label) is equal to category_slug. I.e. for a specific category.
// you can pass arrays
if ( is_tax( array('category','post_tag') , array('tax_name1','tax_name2') ) ) { ... }
// will work if it is a category or label page whose name is tax_name1 or tax_name2
If it's a search results page.
is_404() (404 page)
If it's a 404 error page.
is_attachment() (attachment page)
If this is a separate page for an attached file (usually images).
is_admin() (admin page)
If it's a blog admin page.
is_user_logged_in() (user is logged in)
Triggers if the user is logged in.
current_user_can('capability') (checks user capability)
Checks the user's capabilities to perform a particular action. The user's capability is specified in the function parameter. The full list of capabilities can be found in the documentation.
have_posts() (has posts to display)
If the current WP query returned data for building the loop, in other words, if there are posts on the page.

Conditional tags and checks intended for individual pages or posts

The following conditional tags (checks) will only work on individual pages, in the language of this article, if the condition is met: is_single(), is_page(), or is_singular().

has_tag() (post has tags)

If the post has tags. The function can take a parameter of the tag name or an array of several tag names.

if ( has_tag() ) { ... }           // will work if the post has at least one label
if ( has_tag('tag_name') ) { ... } // works if the post has a tag with the name tag_name
// Option with array
if ( has_tag( array('tag_name','tag_name2') ) ) { ... }
// will work if the post has a tag named tag_name or tag_name2
in_category( 10 ) (post is in category 10)
If the post is in the category with an ID of 10. Can be used inside the loop.
have_comments() (post has comments)
If the post has comments.
pings_open() (pings are open)
If the post accepts pings, i.e., if pings are open.
$wp_query->query_vars[cpage](comments pagination page)
If this is a comments pagination page. (/comment-page-1 /comment-page-2 and so on)
$post->post_excerpt(post has an excerpt)
If the post has an excerpt.
$post->post_parent == 20(parent page ID is 20)
If the ID of the parent page is 20. Works only for static pages. Also useful for attachments (files).

Rare WordPress conditional tags (used seldom or not intended for templates)

  • is_sticky() — Checks if the current post is attached to the top of the home page posts query. Conditional tag.
  • is_comment_feed() — Is the query for a comments feed?
  • is_comments_popup() — Determines whether the current URL is within the comments popup window.
  • is_preview() — Is the query for a post or page preview?
  • is_trackback() — Is the query for a trackback endpoint call?
  • is_feed() — Is the query for a feed?