Home WordPress Conditional Functions is_child_theme() WordPress is_child_theme() Function Definition In WordPress, is_child_theme() returns true when the active theme is a child theme . It returns false when a normal (parent) theme is active.
Theme and plugin developers use it to load extra files or settings only when a child theme is in use. It does not need the main query.
Syntax Use it as the test of an if statement.
if ( is_child_theme() ) {
// code for the true path
} else {
// code for the false path
} Call What it does is_child_theme()True if a child theme is active. ! is_child_theme()True if a parent theme is active. get_stylesheet_directory()The folder of the active theme, which is the child folder when a child theme is active.
Use get_template_directory() for the parent folder and get_stylesheet_directory() for the active theme folder.
Full declaration Parameters Parameter Description None This function takes no parameters.
Return Value Return Value Description bool true if a child theme is active, otherwise false.
WordPress Examples Three short theme examples. Each one shows what prints on the true path and the false path.
1. Print a note about the theme 2. Load a file from the... 3. Load the parent style first Example 1 — Print a note about the theme Tell the developer which type of theme is active.
<?php
if ( is_child_theme() ) {
echo 'Child theme: ' . wp_get_theme()->get( 'Name' );
} else {
echo 'Parent theme: ' . wp_get_theme()->get( 'Name' );
} Child theme active (true):
Child theme: My Child Theme
Parent theme active (false):
Parent theme: Twenty Twenty-Five How It Works is_child_theme() compares the theme and the parent theme.wp_get_theme() gives details of the active theme.The else branch covers the normal theme. Example 2 — Load a file from the right folder Pick the child folder when it exists.
<?php
$dir = is_child_theme() ? get_stylesheet_directory() : get_template_directory();
echo $dir; Child theme (true):
/var/www/wp-content/themes/my-child
Parent theme (false):
/var/www/wp-content/themes/my-parent How It Works The short ? : form picks one of two folders. get_stylesheet_directory() is the active theme folder.get_template_directory() is always the parent folder.Example 3 — Load the parent style first A child theme usually needs the parent stylesheet too.
function mytheme_styles() {
if ( is_child_theme() ) {
wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css' );
}
wp_enqueue_style( 'theme-style', get_stylesheet_uri() );
}
add_action( 'wp_enqueue_scripts', 'mytheme_styles' ); Child theme (true) adds two links:
<link rel="stylesheet" href=".../my-parent/style.css">
<link rel="stylesheet" href=".../my-child/style.css">
Parent theme (false) adds one link:
<link rel="stylesheet" href=".../my-parent/style.css"> How It Works The check runs when WordPress loads styles. A child theme loads the parent stylesheet first. get_stylesheet_uri() is the active theme style.css.Frequently Asked Questions What does is_child_theme() return? It returns true or false (a bool). Use it inside an if statement.
How does WordPress know a theme is a child theme? The child style.css has a Template line that names the parent theme folder.
Does is_child_theme() depend on the page? No. It is the same on every page because it only looks at the active theme.
What is the difference between get_template_directory() and get_stylesheet_directory()? get_template_directory() is the parent theme folder. get_stylesheet_directory() is the active theme folder, so it is the child folder when a child theme is active.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
13 people found this page helpful
Helpful Share Copy link Suggestion