Blog, WordPress, WordPress Development

Conditional Tags in WordPress

WordPress Conditional Tags are a boolean data type that can be used in your Template Files to alter the display of content depending on the conditions that the current page matches. They tell WordPress what code to display under specific conditions.

There are conditional tags for different areas of your website such as your home page, blog posts and pages. This allows you to change what is displayed around your website. For example, you could change your website logo in different areas of your website.

How Conditional Tags Work in WordPress

The code begins by checking to see if a statement is true or false. If the statement is found to be true, the first set of code is executed. If it’s false, the first set of code is skipped, and the second set of code (after the else) is executed instead.

WordPress Conditional Tags List

  • is_front_page: Determines whether the query is for the front page of the site.
if ( is_front_page() ) :
    get_header( 'front' );
else :
    get_header();
endif;
  • is_home: Determines whether the query is for the blog homepage.
if ( is_home() ) {
    // This is the blog posts index
    get_sidebar( 'blog' );
} else {
    // This is not the blog posts index
    get_sidebar();
}
  • is_admin: Determines whether the current request is for an administrative interface page.
if ( ! is_admin() ) {
    echo "You are viewing the theme";
} else {
    echo "You are viewing the WordPress Administration Panels";
}
  • is_network_admin: Whether the current request is for the network administrative interface.
if ( is_network_admin() ) {
    echo __( 'You are viewing a WordPress network administration page', 'textdomain' );
} else {
    echo __( 'You are not viewing a WordPress network administration page', 'textdomain' );
}
if ( is_admin_bar_showing() ) {
    // do something
}
  • is_single: Determines whether the query is for an existing single post.
is_single();
// When any single Post page is being displayed.
is_single('17');
// When Post 17 (ID) is being displayed.
is_single(17);
// When Post 17 (ID) is being displayed. Integer parameter also works
is_single('Irish Stew');
// When the Post with post_title of "Irish Stew" is being displayed.
is_single('beef-stew');
// When the Post with post_name (slug) of "beef-stew" is being displayed.
is_single(array(17,'beef-stew','Irish Stew'));
// Returns true when the single post being displayed is either post ID 17,
// or the post_name is "beef-stew", or the post_title is "Irish Stew".
// Note: the array ability was added in version 2.5.
  • is_sticky: Determines whether a post is sticky.
is_sticky();
// When any Sticky Post page is being displayed.
is_sticky('17');
// When Sticky Post 17 (ID) is being displayed.
<?php if ( is_post_type_archive() ) { ?>
<h1><?php post_type_archive_title(); ?></h1>
<?php } ?>
<?php
function my_function( $query ){
    if ( is_post_type_archive( 'my_custom_post_type' ) ) {
         // Do stuff
    }
}
add_action( 'pre_get_posts', 'my_function' );
?>
  • comments_open: Determines whether the current post is open for comments.
/**
 * Enqueue wpdocs_script if viewing a post with comments enabled.
 */
function wpdocs_scripts(){
    if ( is_single() && comments_open() ) {
        // wpdocs_script must have been previously registered via wp_register_script()
        wp_enqueue_script( 'wpdocs_script' );
    }
}
add_action( 'wp_print_scripts', 'wpdocs_scripts' );
  • pings_open: Determines whether the current post is open for pings.
  • is_page: Determines whether the query is for an existing single page.
// When any single Page is being displayed.
is_page();
 
// When Page 42 (ID) is being displayed.
is_page( 42 );
// When the Page with a post_title of "Contact" is being displayed.
is_page( 'Contact' );
// When the Page with a post_name (slug) of "about-me" is being displayed.
is_page( 'about-me' );
/*
 * Returns true when the Pages displayed is either post ID 42,
 * or post_name "about-me", or post_title "Contact".
 * Note: the array ability was added in version 2.5.
 */
is_page( array( 42, 'about-me', 'Contact' ) );
if ( is_page_template( 'about.php' ) ) {
    // about.php is used
} else {
    // about.php is not used
}
if ( is_page_template( 'directory-name/page-about.php' ) ) {
    // about.php is used
} else {
    // about.php is not used
}
if ( is_page_template( array( 'template-full-width.php', 'template-product-offers.php' ) ) ) {
   // Do Something here if either of the above templates are being used
}
else {
   // Else do this
}
  • is_category: Determines whether the query is for an existing category archive page.
is_category();
// When any Category archive page is being displayed.
is_category( '9' );
// When the archive page for Category 9 is being displayed.
is_category( 'Stinky Cheeses' );
// When the archive page for the Category with Name "Stinky Cheeses" is being displayed.
is_category( 'blue-cheese' );
// When the archive page for the Category with Category Slug "blue-cheese" is being displayed.
is_category( array( 9, 'blue-cheese', 'Stinky Cheeses' ) );
// Returns true when the category of posts being displayed is either term_ID 9,
// or slug "blue-cheese", or name "Stinky Cheeses".
// Note: the array ability was added in version 2.5.
  • is_tag: Determines whether the query is for an existing tag archive page.
// When any Tag archive page is being displayed.
is_tag();
// When the archive page for Tag 30 is being displayed.
is_tag( '30' );
// When the archive page for tag with the Slug of 'extreme' is being displayed.
is_tag( 'extreme' );
// When the archive page for tag with the Name of 'mild' is being displayed.
is_tag( 'mild' );
/*
 * Returns true when the tag of posts being displayed is either term_ID 30,
 * or slug "extreme", or name "mild". Note: the array ability was added
 * at Version 3.7.
 */
is_tag( array( 30, 'mild', 'extreme' ) );
  • is_tax: Determines whether the query is for an existing custom taxonomy archive page.
is_tax();
// When any custom taxonomy archive page is being displayed.
is_tax( 'channel' );
// When the archive page for taxonomy of 'channel' is being displayed.
is_tax( 'channel', 'BBC1' );
// When the archive page for taxonomy of 'channel' is being displayed
// and the 'channel' taxonomy term is 'BBC1'.
  • has_term: Checks if the current post has any of given terms.
if( has_term( 'jazz', 'genre' ) ) {
    // do something
}
  • term_exists: Determines whether a taxonomy term exists.
$term = term_exists( 'Uncategorized', 'category' );
if ( $term !== 0 && $term !== null ) {
    echo __( "'Uncategorized' category exists!", "textdomain" );
}
$taxonomy_exist = taxonomy_exists( 'category' );
// Returns true
$taxonomy_exist = taxonomy_exists( 'post_tag' );
// Returns true
$taxonomy_exist = taxonomy_exists( 'link_category' );
// Returns true
$taxonomy_exist = taxonomy_exists( 'my_taxonomy' );
// Returns false if global $wp_taxonomies['my_taxonomy'] is not set
  • is_author: Determines whether the query is for an existing author archive page.
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".  Note: the array ability was added at Version 2.5.
  • is_date: Determines whether the query is for an existing date archive.
if ( is_date() ) {
    // Do awesome.
}
  • is_year: Determines whether the query is for an existing year archive.
if ( is_year() ) {
    // Do awesome.
}
  • is_month: Determines whether the query is for an existing month archive.
if ( is_month() ) {
    // Do awesome.
}
  • is_day: Determines whether the query is for an existing day archive.
if ( is_day() ) {
    // Do awesome.
}
  • is_time: Determines whether the query is for a specific time.
if ( is_time() ) {
    // Do awesome.
}
  • is_new_day: Determines whether the publish date of the current post in the loop is different from the publish date of the previous post in the loop.
  • is_archive: Determines whether the query is for an existing archive page.
if(is_archive())
    // write your code here ...
  • is_search: Determines whether the query is for a search.
if ( is_search() ) {
    // add external search form (Google, Yahoo, Bing...)
}
  • is_404: Determines whether the query has resulted in a 404 (returns no results).
if ( is_404() ) {
    // add search form so that users can search other posts
}
  • is_paged: Determines whether the query is for a paged result and not for the first page.
  • is_attachment: Determines whether the query is for an existing attachment page.
if ( is_attachment() ) {
    // show adv. #1
} else {
    // show adv. #2
}
$id = 37;
if ( wp_attachment_is_image( $id ) ) {
    printf( 'Post %d is an image!', $id );
} else {
    printf( 'Post %d is not an image.', $id );
}
  • is_singular: Determines whether the query is for an existing single post of any post type (post, attachment, page, custom post types).
if ( is_singular() ) {
  // show adv. #1
} else {
  // show adv. #2
}

is_singular( 'book' );
is_singular( array( 'newspaper', 'book' ) );
$exists = post_type_exists( 'post' );
// returns true
  
$exists = post_type_exists( 'page' );
// returns true
  
$exists = post_type_exists( 'book' );
// returns true if book is a registered post type
  
$exists = post_type_exists( 'xyz' );
// returns false if xyz is not a registered post type
/**
 * If the global query is for a category, exclude category 5.
 *
 * @param WP_Query $query Global WP_Query instance.
 */
function wpdocs_modify_query_exclude_category( $query ) {
    if ( ! is_admin() && $query->is_main_query() && ! $query->get( 'cat' ) )
        $query->set( 'cat', '-5' );
}
add_action( 'pre_get_posts', 'wpdocs_modify_query_exclude_category' );
  • is_new_day: Determines whether the publish date of the current post in the loop is different from the publish date of the previous post in the loop.
  • is_feed: Determines whether the query is for a feed.
  • is_trackback: Determines whether the query is for a trackback endpoint call.
  • is_preview: Determines whether the query is for a post or page preview.
function comments_template( $file = '/comments.php', $separate_comments = false ) {
global $wp_query, $withcomments, $post, $wpdb, $id, $comment, $user_login, $user_ID, $user_identity, $overridden_cpage;
if ( ! ( is_single() || is_page() || $withcomments ) || empty( $post ) ) {
    return;
}
if ( is_preview() ) {
    return;
}
  • in_the_loop: Determines whether the caller is in the Loop.
function wpdocs_modify_single_post_entry_titles( $title ) {
    if ( is_singular( 'post' ) && in_the_loop() ) {
        /* Modify $title */
    }
    return $title;
}
add_filter( 'the_title', 'wpdocs_modify_single_post_entry_titles' );
  • is_dynamic_sidebar: Determines whether the dynamic sidebar is enabled and used by the theme.
<?php if ( is_active_sidebar( 'left-sidebar' ) ) { ?>
    <ul id="sidebar">
        <?php dynamic_sidebar( 'left-sidebar' ); ?>
    </ul>
<?php } ?>
  • is_active_widget: Determines whether a given widget is displayed on the front end.
  • is_rtl: Determines whether the current locale is right-to-left (RTL).
if ( is_rtl() ) {
  wp_enqueue_style(  'style-rtl',  plugins_url( '/css/style-rtl.css', __FILE__ ) );
  wp_enqueue_script( 'script-rtl', plugins_url( '/js/script-rtl.js',  __FILE__ ) );
}
  • is_main_site: Determine whether a site is the main site of the current network.
if ( is_main_site( $blog_id ) ) {
  // display something special for the main site.
}
// Removes the "Edit" menu for users who are not Super Admins of a multisite network
if ( ! is_super_admin() ) {
    add_action( 'admin_init', 'wpdocs_remove_edit_menu' );
}
/**
 * Remove the profile editing link for non-super admins.
 */
function wpdocs_remove_edit_menu() {
    remove_menu_page('edit.php');
}
if ( is_user_logged_in() ) {
    echo 'Welcome, registered user!';
} else {
    echo 'Welcome, visitor!';
}
$email = 'myemail@example.com';
$exists = email_exists( $email );
if ( $exists ) {
    echo "That E-mail is registered to user number " . $exists;
} else {
    echo "That E-mail doesn't belong to any registered users on this site";
}
$username = sanitize_user( $_POST['username'] );
if ( username_exists( $username ) ) {
    echo "Username In Use!";
} else {
    echo "Username Not In Use!";
}
  • is_plugin_active: Determines whether a plugin is active.
/**
 * Detect plugin. For use on Front End only.
 */
include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
// check for plugin using plugin name
if ( is_plugin_active( 'plugin-directory/plugin-file.php' ) ) {
    //plugin is activated
} 
if ( ! function_exists( 'is_plugin_inactive' ) ) {
    require_once( ABSPATH . '/wp-admin/includes/plugin.php' );
}
if ( is_plugin_inactive( 'plugin-directory/plugin-file.php' ) ) {
    //plugin is not activated
}
if ( ! function_exists( 'is_plugin_active_for_network' ) ) {
    require_once( ABSPATH . '/wp-admin/includes/plugin.php' );
}
if ( is_plugin_active_for_network( 'plugin-directory/plugin-file.php' ) ) {
    // Plugin is activated
}
if (current_theme_supports('custom-header')) {
  // do something special when custom-header is supported...
}
if ( has_post_thumbnail() ) {
    the_post_thumbnail();
}
else {
    echo '<img src="' . get_bloginfo( 'stylesheet_directory' ) 
        . '/images/thumbnail-default.jpg" />';
}
  • wp_script_is: Determines whether a script has been added to the queue.
$handle = 'fluidVids.js';
$list = 'enqueued';
if (wp_script_is( $handle, $list )) {
    return;
} else {
    wp_register_script( 'fluidVids.js', plugin_dir_url(__FILE__).'js/fluidvids.min.js');
    wp_enqueue_script( 'fluidVids.js' );
}
  • has_excerpt: Determines whether the post has a custom excerpt.
if ( ! has_excerpt() ) {
    echo '';
} else { 
    the_excerpt();
}

// Get $post if you're inside a function.
global $post;
 
if ( has_excerpt( $post->ID ) ) {
    // This post has excerpt.
} else {
    // This post has no excerpt.
}
  • has_nav_menu: Determines whether a registered nav menu location has a menu assigned to it.
if ( has_nav_menu( 'primary' ) ) {
    wp_nav_menu( array( 'theme_location' => 'primary' ) );
}

WordPress Conditional Tags Tricks

  • is_subpage: There is no is_subpage() function, but you can test this with a little code:
function is_subpage() {
    global $post;
    if ( is_page() && $post->post_parent ) {
        return $post->post_parent;
    } else {
        return false;
    }
}