Skip to content

Instantly share code, notes, and snippets.

@broskees
Forked from stephenharris/is-descendent-of.php
Last active September 2, 2022 19:01
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 broskees/8fd20d3167c4e7dd2941aaef5e7068cc to your computer and use it in GitHub Desktop.
Save broskees/8fd20d3167c4e7dd2941aaef5e7068cc to your computer and use it in GitHub Desktop.
Check if current page is a descendant of the specified one.
<?php
/**
* Is the current page a descendent of page identified by the path (not slug).
* A page is not a descendent of itself!
*
* @link http://wordpress.stackexchange.com/questions/101213/does-is-child-exist-in-wp-3-5-1
* @link https://gist.github.com/broskees/8fd20d3167c4e7dd2941aaef5e7068cc
* @param string $page_path The path of the page, e.g. mypage/mysubpage
* @return boolean True if current page is a descednent of specified $page_path. False otherwise.
*/
function is_descendent_of($page_path){
global $post;
if (!is_page($post)) {
return false;
}
if ($page = get_page_by_path($page_path)) {
return in_array(
$page->ID,
get_post_ancestors($post->ID)
);
}
return false;
}
?>
@broskees
Copy link
Author

broskees commented Sep 2, 2022

PSR-12 version

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment