is_child() Conditional Function for WordPress

I wrote a little function a while back to Check if a Page is a Child of Another Page in WordPress called is_child(). It used the page ID to return a true or false value dependent on if the current page was a child of the parent page specified. I have recently made a little change so that the function could now accept either a page ID or slug.
Here it is again with the changes:

function is_child($page_id_or_slug) {
	global $post;
	if(!is_int($page_id_or_slug)) {
		$page = get_page_by_path($page_id_or_slug);
		$page_id_or_slug = $page->ID;
	}
	if(is_page() && $post->post_parent == $page_id_or_slug ) {
       		return true;
	} else {
       		return false;
	}
}

The modification was inspired by Helpful Page Functions for WordPress.

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