Created
August 1, 2019 22:21
-
-
Save VermillionOne/98b37e44ebf1c326eeaf9ae50e84d406 to your computer and use it in GitHub Desktop.
Check if page or child of given ID
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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