June 15, 2015
// Check if page is direct child
function is_child($page_id) {
global $post;
if( is_page() && ($post->post_parent == $page_id) ) {
return true;
} else {
return false;
}
}
// Check if page is an ancestor
function is_ancestor($post_id) {
global $wp_query;
$ancestors = $wp_query->post->ancestors;
if ( in_array($post_id, $ancestors) ) {
return true;
} else {
return false;
}
}
Add this code to your functions.php file in order to utilize is_child and is_ancestor filters.
Originally sourced by: http://codebyte.dev7studios.com/post/3313452026/wordpress-is-child-and-is-ancestor. This website is unavailable as of August 10, 2017.