How to test if your theme is a Child Theme

- by

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 '
    '; foreach ($allThemes as $theme) { echo '
  1. '; // 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.
  2. '; } else { echo ' is a Child Theme'; } } echo '
';

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.



If you enjoy my content, please consider supporting me on Ko-fi. In return you can browse this whole site without any pesky ads! More details here.

Leave a Comment!

This site uses Akismet to reduce spam. Learn how your comment data is processed.