WordPress Conditional Tags Cheat Sheet With Examples
Conditional tags are functions that return true or false depending on the page being viewed. Use them to show or hide content, or to load scripts only where they are needed.
Page type
is_front_page(): the site front page, whether it shows posts or a static page.is_home(): the blog posts page. This is not the same as the front page.is_single(): a single post of any type except pages and attachments.is_page(): a single page. Accepts an ID, slug or title, likeis_page( 'contact' ).is_singular(): any single post, page or custom post type.is_archive(): any archive page.is_category(),is_tag(),is_tax(): taxonomy archives.is_post_type_archive(): a custom post type archive.is_author(): an author archive.is_search(): search results.is_404(): the not found page.is_paged(): any page after the first in a paginated list.
Content and templates
has_post_thumbnail(): the post has a featured image.has_term(): the post has a given term.in_category(): the post belongs to a category.is_sticky(): the post is sticky.has_nav_menu(): a menu is assigned to a location.is_active_sidebar(): the widget area has widgets.is_page_template(): the page uses a specific template.
User and environment
is_user_logged_in(): the visitor is logged in.is_admin(): the request is for the dashboard.is_customize_preview(): the Customizer preview is open.is_preview(): a post preview.is_main_query(): the current query is the main one.wp_is_mobile(): a mobile browser. Careful with page caching.is_ssl(): the page is served over HTTPS.is_multisite(): the install is a multisite network.is_rtl(): the language is right to left.
Examples
// Load a script only on the contact page.
function lotgrafix_conditional_assets() {
if ( is_page( 'contact' ) ) {
wp_enqueue_script( 'lotgrafix-contact', get_template_directory_uri() . '/js/contact.js', array(), '1.0.0', true );
}
}
add_action( 'wp_enqueue_scripts', 'lotgrafix_conditional_assets' );
// Show a message only to logged in users.
if ( is_user_logged_in() ) {
echo '<p>Welcome back!</p>';
}
// Show a sidebar only when it has widgets.
if ( is_active_sidebar( 'lotgrafix-sidebar' ) ) {
dynamic_sidebar( 'lotgrafix-sidebar' );
}
// Combine conditions.
if ( is_singular( 'post' ) && has_post_thumbnail() ) {
the_post_thumbnail( 'large' );
}
Important notes
- Conditional tags only work after WordPress has built the query, so use them inside hooks like
wp_enqueue_scriptsortemplate_redirect, not earlier. - Most of them accept optional arguments to match a specific item.


