Skip to content

Instantly share code, notes, and snippets.

@jhebb
Last active July 19, 2023 14:54
Show Gist options
  • Save jhebb/4661007 to your computer and use it in GitHub Desktop.
Save jhebb/4661007 to your computer and use it in GitHub Desktop.
WordPress is_subpage() function to check if a page against it's immediate parent or all it's parents
/**
* Is SubPage?
*
* Checks if the current page is a sub-page and returns true or false.
*
* @param $page mixed optional ( post_name or ID ) to check against.
* @param $single boolean optional - default true to check against all parents, false to check against immediate parent.
* @return boolean
*/
function is_subpage( $page = null, $all = true ) {
global $post;
// is this even a page?
if ( ! is_page() )
return false;
// does it have a parent?
if ( ! isset( $post->post_parent ) OR $post->post_parent <= 0 )
return false;
// is there something to check against?
if ( ! isset( $page ) ) {
// yup this is a sub-page
return true;
} else {
// if $page is an integer then its a simple check
if ( is_int( $page ) ) {
// check
if ( $post->post_parent == $page )
return true;
} else if ( is_string( $page ) ) {
// get ancestors
$parents = get_ancestors( $post->ID, 'page' );
// does it have ancestors?
if ( empty( $parents ) )
return false;
if ( $all == true ) { // check against all parents
// loop through ancestors
foreach ( $parents as $parent ) {
$parent = get_post( $parent );
if ( is_page() && $parent->post_name == $page) {
return true;
}
}
} else { // check against immediate parent
// get the first ancestor
$parent = get_post( $parents[0] );
// compare the post_name
if ( $parent->post_name == $page )
return true;
}
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment