How to test if your theme is a Child Theme

wordpress-icon
You may need to know which of your themes are child themes, or in fact if the current theme you’re using is a parent or a child theme.

Here’s how you can test both options.

The following code snippet will iterate through all themes that are currently installed and displays the title and if it is a child theme or not:

$allThemes = wp_get_themes();
echo '<ol>';
foreach ($allThemes as $theme) {
	echo '<li>';

	// print the theme title
	echo $theme->get('Name');

	// determine whether it's a child theme or not
	if ($theme->parent() == false) {
		echo ' is not a Child Theme.</li>';
	} else {
		echo ' is a Child Theme</li>';
	}
}
echo '</ol>';

First we grab an array of installed themes using wp_get_themes(). Each item is an object of WP_Theme and has many helpful methods. Its method parent() will return false for non-child themes, or the parent theme if it is in fact a child theme.

Next we test if the output is false, and if so print a status accordingly.

Is the current theme a child theme?

Here’s how we can check it:

// is the current theme a child theme?
$currentTheme = wp_get_theme();
if ($currentTheme->parent() == false) {
	echo 'The current theme is not a child theme.';
} else {
	echo 'The current theme is a child theme';
}

Here we employ very much the same, except for the first line in which we grab only a single object which defaults to the current theme.

Check out all the other bits of info this class can provide.

You can leave a comment on my original post.