WordPress ships with a rich set of conditional tag functions that let you check exactly where a visitor is on your site and run code only in the right context. Functions like is_single(), is_page(), is_archive(), is_home(), and is_front_page() are the building blocks of almost every theme customisation. Without them, a snippet you add to functions.php or a template file would fire on every page load, causing unexpected side-effects across your entire site. Conditional tags are especially important when you enqueue scripts and styles — loading a heavy JavaScript library only on the pages that need it saves bandwidth and speeds up every other page. You can pass an ID, slug, or title as an argument to most conditional tags: is_page( 'about' ) returns true only on the page with the slug about. The is_singular() function accepts an array of post types so you can match multiple types at once. is_tax(), is_category(), and is_tag() help you scope code to taxonomy archive pages. Combining conditional tags with the hook system — for example, hooking into wp_head only when is_single() is true — is a core pattern in professional WordPress development. The is_admin() check is arguably the most commonly used conditional of all, since nearly every front-end snippet must exclude the dashboard. Conditional tags are evaluated after the main query runs, so they work reliably inside template files and wp action hooks but not inside plugins_loaded or init where the query has not yet fired. This guide lists the most useful conditionals with concise code examples.
Problem: You need to run PHP or load assets only on specific pages, post types, or archive screens, and the built-in conditional tag names and arguments are not obvious.
Solution: Add the following reference examples to your functions.php file:
// Single post of any type
if ( is_single() ) { /* runs on any single post */ }
// Specific post by ID, slug or title
if ( is_single( 42 ) ) { /* only post ID 42 */ }
if ( is_single( 'my-post-slug' ) ) { /* only that slug */ }
// Pages
if ( is_page() ) { /* any page */ }
if ( is_page( [ 'about', 'contact' ] ) ) { /* about OR contact page */ }
// Front page vs blog index
if ( is_front_page() ) { /* static front page or blog index depending on settings */ }
if ( is_home() ) { /* the blog posts index page */ }
// Custom post types
if ( is_singular( 'product' ) ) { /* single WooCommerce product */ }
if ( is_post_type_archive( 'product' ) ) { /* WooCommerce shop archive */ }
// Taxonomy archives
if ( is_category( 'news' ) ) { /* category archive */ }
if ( is_tag( 'tutorial' ) ) { /* tag archive */ }
if ( is_tax( 'genre' ) ) { /* custom taxonomy archive */ }
// Search results and 404
if ( is_search() ) { /* search results page */ }
if ( is_404() ) { /* 404 not found page */ }
// Admin vs front-end
if ( is_admin() ) { /* WordPress dashboard */ }
if ( ! is_admin() ) { /* front-end only */ }
// Logged-in users
if ( is_user_logged_in() ) { /* show content only to authenticated users */ }
// Practical example: load a script only on single posts
add_action( 'wp_enqueue_scripts', 'helloadmin_conditional_script' );
function helloadmin_conditional_script() {
if ( is_single() ) {
wp_enqueue_script( 'my-reading-progress', get_template_directory_uri() . '/js/reading-progress.js', [], '1.0', true );
}
}
NOTE: is_front_page() and is_home() are often confused. When your front page displays your latest posts, both return true on the home page. When you set a static front page in Settings → Reading, is_front_page() returns true on that static page and is_home() returns true on the separate blog index page. Always test both when customising the home page of a WordPress site.