Site icon WP Smith

How to Determine Child/Ancestor with is_child() and is_ancestor()

I found this great piece of code at Codebyte.
Put the following in your functions.php file:
[php]
// Check if page is direct child
function wps_is_child( $page_id ) {
global $post;
if( is_page() && ( $post->post_parent == $page_id ) ) {
return true;
} else {
return false;
}
}
[/php]
[php]
// Check if page is an ancestor
function wps_is_ancestor( $post_id ) {
global $wp_query;
$ancestors = $wp_query->post->ancestors;
if ( in_array( $post_id, $ancestors ) ) {
return true;
} else {
return false;
}
}
[/php]