“Show the slider only on the front page.” “Load this CSS only on product pages.”—conditional tags are how you check what kind of page is currently being displayed. You use them by dropping them into an if.
<?php if (is_front_page()) : ?>
<!-- content only for the front page -->
<?php endif; ?>Commonly used conditional tags
| Tag | Pages where it’s true |
|---|---|
is_front_page() | The site’s front page (the top of the URL) |
is_home() | The post archive page |
is_single() | A post’s single page (is_single('slug') narrows it further) |
is_page() | A static page (is_page('about'), is_page([1,2])) |
is_singular() | Every single view—posts, static pages and custom post types |
is_archive() | Every archive view (category, tag, date, post type) |
is_category() | A category archive (is_category('news')) |
is_tag() | A tag archive |
is_tax() | A custom taxonomy archive (is_tax('season')) |
is_post_type_archive() | A custom post type archive (is_post_type_archive('shohin')) |
is_search() | Search results |
is_404() | A page that wasn’t found |
is_paged() | Page 2 onward |
in_category() | Whether the current post belongs to a category (in_category('news')) |
has_term() | Whether the current post has a term (has_term('winter', 'season')) |
is_user_logged_in() | Whether someone is logged in |
is_admin() | Whether this is the admin screen |
The classic stumble: is_home and is_front_page
The confusion comes from the names not lining up with reality.
is_front_page()—the site’s entrance (https://example.com/)is_home()—the post archive page
The relationship changes with “Settings” → “Reading.”
| Reading setting | Front page URL | Post archive URL |
|---|---|---|
| Your homepage displays = your latest posts | is_front_page() and is_home() are both true | The same page |
| Your homepage displays = a static page | Only is_front_page() is true | is_home() is true on the “posts page” you designated |
If you want “front page only,” use is_front_page(). Use is_home() there and it stops working the moment you switch to a static front page.
Choosing among the single-page tags
<?php if (is_single()) : ?> <!-- a post's single page (not static pages) -->
<?php if (is_page()) : ?> <!-- a static page -->
<?php if (is_singular('shohin')) : ?> <!-- a product's single page -->
<?php if (is_singular()) : ?> <!-- any single page at all -->is_single() covers posts and custom post types, not static pages. When you want to bundle “every single page,” is_singular() is the handy one.
Example 1: load CSS and JS per page
Branching in functions.php means you only load things on the pages that need them, which makes the site lighter.
function my_scripts() {
wp_enqueue_style('main', get_theme_file_uri('style.css'), [], '1.0');
if (is_front_page()) {
wp_enqueue_script('slider', get_theme_file_uri('js/slider.js'), [], '1.0', true);
}
if (is_singular('shohin')) {
wp_enqueue_style('product', get_theme_file_uri('css/product.css'), [], '1.0');
}
}
add_action('wp_enqueue_scripts', 'my_scripts');Theme developmentWiring up functions.php and CSS the right wayThe basics of loading CSS and JS (wp_enqueue)Example 2: vary the page title from one place
<h1 class="page-title">
<?php if (is_front_page()) : ?>
Shimaenagado
<?php elseif (is_home()) : ?>
News
<?php elseif (is_category() || is_tag() || is_tax()) : ?>
<?php echo esc_html(single_term_title('', false)); ?>
<?php elseif (is_post_type_archive()) : ?>
<?php echo esc_html(post_type_archive_title('', false)); ?>
<?php elseif (is_search()) : ?>
Search results for "<?php echo esc_html(get_search_query()); ?>"
<?php elseif (is_404()) : ?>
Page not found
<?php else : ?>
<?php the_title(); ?>
<?php endif; ?>
</h1>The order of the branches matters. Put a broad condition (is_archive()) first and the narrower ones (is_category()) never get reached. Line them up from most specific to least.
Example 3: change the display only for posts in a category
in_category() checks “the current post” inside the loop.
<?php if (in_category('event')) : ?>
<p class="notice">Events require registration to attend.</p>
<?php endif; ?>For a custom taxonomy, it’s has_term().
<?php if (has_term('winter', 'season')) : ?>
<span class="badge">Winter only</span>
<?php endif; ?>Combining and excluding
<?php if (is_singular() && !is_front_page()) : ?> <!-- a single page, but not the front page -->
<?php if (is_page(['about', 'company'])) : ?> <!-- several static pages -->
<?php if (is_home() || is_archive()) : ?> <!-- every archive view -->With ! (negation) plus && and ||, you can express most requirements.
Summary
- Conditional tags just go in an
if. The front page isis_front_page(), the post archive isis_home() - For single views, choose between
is_single()(posts),is_page()(static) andis_singular()(everything) is_category()is the page type,in_category()is the post’s membership—mixing them up is a classic stumble- Put the most specific conditions first. In
functions.php, call them inside a hook
Once you can vary output freely, you can meet requirements without growing the number of template files.