Posted By

depiction on 02/05/13


Tagged

wordpress ischild isancestor ispage


Versions (?)

WordPress is_child() and is_ancestor()


 / Published in: PHP
 

URL: http://codebyte.dev7studios.com/post/3313452026/wordpress-is-child-and-is-ancestor

Put the following in your functions.php file:

  1. // Check if page is direct child
  2. function is_child($page_id) {
  3. global $post;
  4. if( is_page() && ($post->post_parent == $page_id) ) {
  5. return true;
  6. } else {
  7. return false;
  8. }
  9. }
  10.  
  11. // Check if page is an ancestor
  12. function is_ancestor($post_id) {
  13. global $wp_query;
  14. $ancestors = $wp_query->post->ancestors;
  15. if ( in_array($post_id, $ancestors) ) {
  16. return true;
  17. } else {
  18. return false;
  19. }
  20. }

Report this snippet  

You need to login to post a comment.