WordPress.org

Forums

[resolved] Detect grandchildren (8 posts)

  1. mariostella
    Member
    Posted 4 years ago #

    How do you detect if a page is a grandchild and execute some code only on that page and not on the grandpa or the parent?
    Thanks!

  2. keesiemeijer
    moderator
    Posted 4 years ago #

  3. mariostella
    Member
    Posted 4 years ago #

    Thanks but that does not answer my question. I need to know if a page is a grandchild not a child. The codex snippets only talk consider one level of separation, I need two.

    Why doesn't the core include a nice "is_grandchild" function?

  4. mariostella
    Member
    Posted 4 years ago #

  5. keesiemeijer
    moderator
    Posted 4 years ago #

  6. mariostella
    Member
    Posted 4 years ago #

    Hi, Thanks but I had already tried that, and it did not work. However I am not sure I understood the following comment from the original poster and therefore modified the code appropriately.
    This is the comment quote:
    "Why thank you! I had to Booleanate the function (renamed to is_grandchild) and changed line 7 to "return true". – "

    What I did, I renamed the function, then applied the change on line 7 but it did not work the same. I am not sure I understand what he means for Booleanate.

  7. keesiemeijer
    moderator
    Posted 4 years ago #

    a boolean function returns TRUE or FALSE. so you can test it with (on a Page):

    if (get_grandpapa($posts[0]->ID)) {
    // do stuff
    }
  8. mariostella
    Member
    Posted 4 years ago #

    AH! Thanks! I added this

    function get_grandpapa($page_id){
        $current_page = get_page( $page_id );
        if ($current_page->post_parent > 0){
            $parent_page = get_page($current_page->post_parent);
            if ($parent_page->post_parent > 0){
                return $parent_page->post_parent;
            }else{return false;}
        }return false;};

    in the functions file, the called the boolean in the template file. Thanks it works!

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.