WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Depth of $post->post_parent (7 posts)

  1. dressedinvalue
    Member
    Posted 5 years ago #

    I have $post->post_parent=="77" checking to see if the current page is a child of the page id 77. Is there a way to check if the current page is another level deep (sub-sub-page)?

  2. MichaelH
    Member
    Posted 5 years ago #

    Couldn't you use the template tag, wp_list_pages(), along with the child_of and echo=0 parameters?

    Didn't test this, but something like:

    if ( wp_list_pages('echo=0&child_of=' . post->ID) ) {
    // the page has children
    }
  3. Samuel Wood (Otto)
    Tech Ninja
    Posted 5 years ago #

    Are you wanting to see if the grandparent of the post is 77? Why not just check all the way?

    $ancestors = get_post_ancestors($post);
    if (in_array(77,$ancestors)) echo "77 is an ancestor of the post";
  4. dressedinvalue
    Member
    Posted 5 years ago #

    @Otto42 you're practically a magician! This works perfectly. Thank you very much!

    @MichaelH Your code works if I'm only testing for one 'set' of conditions. (See this over here.) Thanks for your help, though!

  5. karl19
    Member
    Posted 5 years ago #

    That bit of code from Otto42 works great, helped me limit the retrieved pages in a custom query. Thanks!

  6. romasse
    Member
    Posted 4 years ago #

    Hey MichalelH,

    You look to be clever on this question. May i ask you a similar question?

    I would like to have a menu with theses caracteristics:

    - If the present page has a parent and no children, show a menu with its parent and him.

    - If the present page has a parent and children, show a menu with its parent,him and its children.

    - If the present page doesn't have a parent (so it is a top level page), show a menu with its children only.

    I have been stucking on it for ages.

    Your help would be very appreciated.
    Thx

  7. Ryan
    Member
    Posted 4 years ago #

    @Otto42: Fantastic. Just fantastic.

    That really should be added to the page in the Codex dealing with creating sub-page conditionals.

Topic Closed

This topic has been closed to new replies.

About this Topic