In WordPress, you can use PHP if statements in theme and template files to handle logic based on the type of page being viewed. This mechanism is called “Conditional Tags.” These functions return true on pages that match the condition and false otherwise, allowing you to insert content or processes only where needed. Conditional tags are written in PHP files. Note that PHP is not executed in the post editor screen or within widgets, so they will have no effect there.

This article introduces beginner-friendly usage and practical examples of representative conditional tags. Everything covered here is explained on the assumption that you’ll use it in theme or child theme PHP files.

What are WordPress conditionals?

WordPress conditional tags are a set of functions that check whether the currently displayed page matches certain criteria. These functions return a boolean (true/false) and are used together with PHP if statements. For example, you can easily implement behavior like “show a slider on the front page and a banner on all other pages.” You can also combine multiple conditions or target specific post IDs and slugs.

Some tags change their target depending on your WordPress reading settings. For example, is_home() identifies the posts archive page, but if the homepage is set to “Your latest posts,” it returns true on the front page. Conversely, if the homepage is set to a static page, it returns true when viewing the static page assigned as the posts page. In this way, you need to use these tags while taking WordPress settings into account.

Basic usage of conditional tags

Front page and posts archive

TagUsePoints
is_front_page()Determines whether the current page is the site’s front page.Use when you want logic only on the front page (a fixed page set as the home).
is_home()Determines whether the current page is the posts archive.If “Your latest posts” is set for the homepage, this returns true on the front page.

Using both together lets you detect a page that is both the “blog posts index” and the “front page.” Regarding the difference between is_home() and is_front_page(): when the homepage is set to “Your latest posts,” is_home() returns true on the front page; when the homepage is set to a “static page,” is_home() returns false, so it’s common to combine it with is_front_page().

Posts and Pages

TagUsePoints
is_single()Determines whether the current page is a single post.You can pass a post ID, slug, or array to target specific posts. Returns true for custom post type single pages as well.
is_page()Determines whether the current page is a static page.You can pass a page ID, slug, or array to target specific pages.
is_singular()Determines whether the current page is a single post, page, or attachment.Useful when you want to branch logic for any single view across post types.

Archives, categories, tags, etc.

TagUsePoints
is_archive()Determines whether the current page is an archive (category, tag, date, author, custom taxonomy, custom post type, etc.).No arguments; use for generic archive detection.
is_category()Determines whether the current page is a category archive.Pass a category ID or slug to target a specific category.
in_category()On single posts, determines whether the post belongs to a specific category.Use when switching what’s displayed per category on single posts.
is_tag()Determines whether the current page is a tag archive.Pass a tag ID or slug to target a specific tag.
has_tag()On single posts, determines whether the post has a specific tag.Use when you want to change design by tag.
is_search()Determines whether the current page is the search results page.You can also get the search query with get_search_query().
is_author()Determines whether the current page is an author archive.Pass a user ID or slug to target a specific author.
is_date() / is_year() / is_month() / is_day()Determines whether the current page is a date-based archive (year, month, day).is_date() includes all (day/month/year) date archives.
is_404()Determines whether the current page is a 404 page.Use when you want a dedicated layout for 404 pages.
wp_is_mobile()Determines whether the visitor is on a mobile device.Useful when switching display between mobile and desktop.
is_user_logged_in()Determines whether the user is logged in.Use when displaying content for logged-in users.

How to use conditionals on pages

On static pages, use is_page(). If you call the function without arguments, it targets all pages.

<?php if ( is_page() ) : ?>
<!-- Process to run on all pages -->
<?php endif; ?>

If you want to target a specific page ID or slug, pass it as an argument. To check multiple pages at once, pass an array.

<?php if ( is_page(97) ) : ?>
<!-- Process when the page ID is 97 -->
<?php endif; ?>

<?php if ( is_page('about') ) : ?>
<!-- Process when the page slug is "about" -->
<?php endif; ?>

<?php if ( is_page(array(97, 103, 234)) ) : ?>
<!-- Process when the page ID is 97, 103, or 234 -->
<?php endif; ?>

To check multiple slugs together, specify them in an array.

<?php if ( is_page(array('about', 'contact', 'service')) ) : ?>
<!-- Process when the slug is "about", "contact", or "service" -->
<?php endif; ?>

You can also mix IDs and slugs.

<?php if ( is_page(array(97, 'about', 'service')) ) : ?>
<!-- Process when the ID is 97, or the slug is "about" or "service" -->
<?php endif; ?>

If you want to target child pages beneath a parent page identified by slug, it’s hard with is_page() alone, so it’s common to combine is_page() with is_page_template() or a custom is_tree() function (defining is_tree() as a custom helper is a typical approach).

How to use conditionals on posts

For posts (e.g., blog articles), use is_single(). The following runs logic on all single posts.

<?php if ( is_single() ) : ?>
<!-- Process to run on all single posts -->
<?php endif; ?>

If you want to target specific posts, pass post IDs or slugs as arguments. To target multiple posts, pass an array.

<?php if ( is_single(97) ) : ?>
<!-- Process when the post ID is 97 -->
<?php endif; ?>

<?php if ( is_single(array(97, 103, 234)) ) : ?>
<!-- Process when the post ID is 97, 103, or 234 -->
<?php endif; ?>

is_single() also returns true on custom post type single pages. If you only want to detect a specific custom post type, use is_singular('custom_post_type').

Conditionals on archive pages

Archive pages refer broadly to listing pages such as category, tag, date, and author archives. Use is_archive() for a generic check.

To detect a specific archive, use dedicated tags: is_category() for category lists, is_tag() for tag lists, is_date() for date archives, and is_author() for author archives. To target only certain categories or tags, specify IDs or slugs. For example, is_category('wordpress') targets only the archive page for the “wordpress” category.

To run logic only on the first page of an archive, combine with !is_paged().

<?php if ( is_category() && !is_paged() ) : ?>
<!-- Process only on the first page of a category archive -->
<?php endif; ?>

Conditionals with custom post types

You can also branch logic on archives and single pages for custom post types you’ve registered (e.g., News, Product info).

Custom post type archives

Use is_post_type_archive() on custom post type archive pages. Passing the post type slug limits detection to that archive. For multiple types, pass an array.

<?php if ( is_post_type_archive('news') ) : ?>
<!-- Archive page for the "news" custom post type -->
<?php endif; ?>

<?php if ( is_post_type_archive(array('news', 'product')) ) : ?>
<!-- Archives for "news" and "product" -->
<?php endif; ?>

If you omit the argument, it returns true for archives of any custom post type.

Custom taxonomies and templates

To detect archives for custom taxonomies, use is_tax(). Pass the taxonomy name as the first argument and the term ID or slug as the second.

<?php if ( is_tax('news_category') ) : ?>
<!-- Archive for the "news_category" custom taxonomy -->
<?php endif; ?>

<?php if ( is_tax('news_category', 97) ) : ?>
<!-- Term archive for ID 97 of the "news_category" taxonomy -->
<?php endif; ?>

To detect a custom page template on pages, use is_page_template(). By specifying the template file name as an argument, you can run logic only on pages where that template is applied.

Combining conditionals

To combine multiple conditions, use PHP logical operators && (AND) and || (OR). For example, to limit logic to “mobile devices and single posts,” write:

<?php if ( wp_is_mobile() && is_single() ) : ?>
<!-- Process for single posts on mobile devices -->
<?php endif; ?>

Use || when you want to run logic if any of multiple conditions match.

<?php if ( wp_is_mobile() || is_single() ) : ?>
<!-- Process for mobile devices or for single posts -->
<?php endif; ?>

For negation, prefix the function with !. For example, to run logic “on pages other than single posts,” write if ( !is_single() ) :.

Common practical examples

Load CSS only on specific pages

By using conditionals in the header, you can switch which stylesheets or JavaScript files are loaded per page. Below is an example that loads different CSS only on the front page or specific pages.

<!-- Load shared styles -->
<link rel="stylesheet" href="/assets/css/common.css" />

<?php if ( is_front_page() && is_page() ) : ?>
<link rel="stylesheet" href="/assets/css/front.css" />
<?php elseif ( is_page('about') ) : ?>
<link rel="stylesheet" href="/assets/css/about.css" />
<?php elseif ( is_page('contact') ) : ?>
<link rel="stylesheet" href="/assets/css/contact.css" />
<?php endif; ?>

By combining multiple conditions with OR, you can also load the same CSS for groups of pages that share a layout, such as the posts archive or category lists.

Change design by category

If you want to change the design of single posts by category, use in_category(). For example, you can display a large featured image for posts in the “news” category and use the regular layout for others.

<?php if ( in_category('news') ) : ?>
    <div class="hero-image">
        <?php the_post_thumbnail('large'); ?>
    </div>
<?php else : ?>
    <div class="thumbnail">
        <?php the_post_thumbnail('medium'); ?>
    </div>
<?php endif; ?>

Hide the sidebar on custom post type archives

You can also easily change layouts such as “don’t show the sidebar on the archive page for a specific custom post type.”

<?php if ( is_post_type_archive('product') ) : ?>
<!-- Do not load the sidebar -->
<?php else : ?>
<?php get_sidebar(); ?>
<?php endif; ?>

Notes and best practices when using conditionals

Coding notes

  • Write them in PHP files – Conditionals are intended for use in PHP files. They won’t run if written in post content or widgets.
  • Mind half-width symbols and syntax – Add a colon after if () and elseif (), and add a semicolon after endif. Avoid full-width spaces or symbols, which can cause errors.
  • Match parentheses – Always ensure the counts of ( and ) match, and use proper indentation for readability.
  • Leave comments – Comment your code to clarify what each block does so it’s easier to understand later.

Theme editing best practices

Editing a WordPress theme directly risks having your customizations overwritten during updates. Therefore, it’s recommended to use a child theme when modifying an existing theme. Even when editing a custom theme, take a backup beforehand, and put feature-level customizations in plugins rather than the theme. For third-party themes, don’t edit the parent theme directly—create a child theme and apply changes there.

As conditional logic grows and becomes complex, code readability tends to suffer. Where appropriate, extract functions or split logic into template parts for better management. Excessively combining conditions can also impact performance, so reduce unnecessary checks and optimize alongside caching and theme best practices.

Summary

WordPress conditional tags are handy functions for branching logic by page type—front page, single posts, pages, archives, and more. Use is_front_page() and is_home() to detect the front page, and is_single() or is_page() to detect single views. There are also conditionals for categories, tags, and custom post types, and you can combine multiple conditions with logical operators.

By using conditionals effectively, you can load CSS or JavaScript only on specific pages or switch designs by category for flexible customization. Just remember to write them in PHP files, follow syntax rules with half-width symbols, and customize safely using child themes and backups.