Skip to content

Instantly share code, notes, and snippets.

@kjbrum
Last active May 21, 2016 02:22
Show Gist options
  • Save kjbrum/56f73a0e710194d8f758 to your computer and use it in GitHub Desktop.
Save kjbrum/56f73a0e710194d8f758 to your computer and use it in GitHub Desktop.
Check if we are on a child or grandchild page of the page.
<?php
/**
* Check if we are on a child or grandchild page of the page.
*
* @param integer $id The ID of the page we're looking for pages underneath
* @param boolean $parent Whether to check if we are on the parent page as well
* @return boolean
*/
function wp_is_child( $id=0, $parent=false ) {
global $post;
if( is_page() ) {
if( $parent && is_page( $id ) ) {
return true; // On the parent page
} else {
$parents = get_post_ancestors( $post->ID );
foreach( $parents as $page_id ){
if( $page_id == $id ){
return true; // At a sub page of $id
}
}
}
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment