A check for if is parent page, if has children, if has grandchildren

Before reading the solution Bravokeyl provided I had finally, through trial and error, come up with a solution that worked for me. I’m not sure which is the better of the two, or the most correct, I only know that mine worked for me, for the problem I had.

This is the code I used to display full-width layout or sidebar-menu layout:

if( is_page() && $post->post_parent > 0 ) { 
  // post has parents

  $children = get_pages('child_of=".$post->ID);
  if( count( $children ) != 0 ) {
    // display sidebar-menu layout
  }

  $parent = get_post_ancestors($post->ID);
  if( count( $children ) <= 0  && empty($parent[1]) ) {
    // display full-width layout
  } elseif ( count( $children ) <= 0  && !empty($parent[1]) )  {
    // display sidebar-menu layout
  }

} else {
  // post has no parents
  // display full-width layout
}

Leave a Comment