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]
Pippin says
Great functions (I’ve used them several times), but please remember to prefix the function names with some unique prefix. This helps prevent function conflicts.
Travis Smith says
Thanks Pippin. Updated the post as we always should use namespaces!
Pippin says
Nice, much better 🙂