A conditional function to check if the current page is a descendant of the ID given to it. Useful for determining if a page is a grandchild, great-grandchild or father down the hierarchy tree.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | // $pid = The ID of the page we're looking for pages underneath function is_tree($pid) { global $post; // load details about this page $anc = get_post_ancestors( $post->ID ); foreach($anc as $ancestor) { if(is_page() && $ancestor == $pid) { return true; } } if(is_page()&&(is_page($pid))) return true; // we're at the page or at a sub page else return false; // we're elsewhere }; |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.