Skip to content

Instantly share code, notes, and snippets.

@VermillionOne
Created August 1, 2019 22:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save VermillionOne/98b37e44ebf1c326eeaf9ae50e84d406 to your computer and use it in GitHub Desktop.
Save VermillionOne/98b37e44ebf1c326eeaf9ae50e84d406 to your computer and use it in GitHub Desktop.
Check if page or child of given ID
/**
* Check whether we are on this page or a sub page
* From WP Codex https://developer.wordpress.org/reference/functions/is_page/
*
* @param int $pid Page ID to check against.
* @return bool True if we are on this page or a sub page of this page.
*/
function wpdocs_is_tree( $pid ) { // $pid = The ID of the page we're looking for pages underneath
$post = get_post(); // load details about this page
$is_tree = false;
if ( is_page( $pid ) ) {
$is_tree = true; // we're at the page or at a sub page
}
$anc = get_post_ancestors( $post->ID );
foreach ( $anc as $ancestor ) {
if ( is_page() && $ancestor == $pid ) {
$is_tree = true;
}
}
return $is_tree; // we arn't at the page, and the page is not an ancestor
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment