这将检查后代关系,而不是严格的父子关系:
/**
* Is the current page a descendent of page identified by the path (not slug).
* A page is not a descendent of itself!
* @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;
}
对于严格的父子检查:
/**
* Is the current page a child of the page identified by the path (not slug).
* A page is not a child of itself! Child != descendant.
* @param string $page_path The path of the page, e.g. mypage/mysubpage
* @return boolean True if current page is a child of specified $page_path. False otherwise.
*/
function is_child_of( $page_path ){
if( !is_page() ){
return false;
}
$page = get_page_by_path( $page_path );
$current_page = get_post();
if( $page )
return $current_page->post_parent == $page->ID;
return false;
}