Skip to content

Instantly share code, notes, and snippets.

@stephenharris
Created May 29, 2013 23:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save stephenharris/5674643 to your computer and use it in GitHub Desktop.
Save stephenharris/5674643 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
* @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 ){
if( !is_page() ){
return false;
}
$ancestors = get_post_ancestors( get_queried_object_id() );
$page = get_page_by_path( $page_path );
if( $page )
return in_array( $page->ID, $ancestors );
return false;
}
?>
@SimonFricker
Copy link

How can this be modified to check the slug?

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