Check if a Page is a Child of Another Page in WordPress

This post has been updated in “is_child() Conditional Function for WordPress”.
WordPress has some default conditional tags that are really helpful when developing themes and plugins that need specific functions on specific pages. Strange enough though, there is no way to check if a page is a child of another page.

This small function checks the database to see if the page happens to have a parent page assigned to it. If so, then the function will return true.

function is_child($pageID) {
	global $post;
	if( is_page() && ($post->post_parent==$pageID) ) {
               return true;
	} else {
               return false;
	}
}

Place the above code in your theme’s functions.php file, or create a functions.php file and place it in your theme’s directory. Then you can use the is_child() function anywhere in your theme. Your code would look something like this:


Share this:

Email
Facebook
Twitter
Pinterest
Pocket

Premium Themes for WordPress

Looking for an easy-to-use Premium Theme for WordPress? Check out Themes by bavotasan.com and have your site up and running in no time.

Use this WordPress website builder to build powerful websites in no time for your or your clients.

WordPress Hosting

WP Engine – designed from the ground-up to support and enhance the workflow of web designers.

Bluehost – providing quality web hosting solutions since 1996.

About the author

Picture of Luke Perrie

Luke Perrie