WordPress.org

Ready to get started?Download WordPress

Forums

is page grandchild ? (15 posts)

  1. berchman
    Member
    Posted 2 years ago #

    I need to test if a page is a grandchild of a specific page

    if ( is_grandchild('8')) {....

    is what I need. But how do I do that with what WordPress has to offer?

  2. keesiemeijer
    moderator
    Posted 2 years ago #

    Try it with something like this:

    <?php
    $ancs = get_ancestors($post->ID, 'page');
    if( count($ancs) == 2 && (in_array(8 ,$post->ancestors))) {
    // do stuff
    echo 'this is a grandchild of top most parent 8';
    }
    ?>
  3. alchymyth
    The Sweeper & Moderator
    Posted 2 years ago #

    a twist to the answer of @keesiemeijer:

    the page could be a great-grandchild of something, and still be grandchild of '8' - try:

    <?php
    $ancs = get_ancestors($post->ID, 'page');
    if( $ancs[1] == 8 ) {
    // do stuff
    echo 'this is a grandchild of page 8';
    }
    ?>

    http://codex.wordpress.org/Function_Reference/get_ancestors

  4. berchman
    Member
    Posted 2 years ago #

    @keesiemeijer and @alchymyth thanks for the suggestions.
    Going to try them out and report back.
    Cheers!

  5. berchman
    Member
    Posted 2 years ago #

    Interesting.

    I tried both options and neither seemed to produce the result.

    I did change the syntax of the "if" conditional to a specific page id to see if the rest of my function works and it does. So the "if" statements above do not display the results of function because the conditional is not correct.

    Any other ideas?

    Thanks!

  6. keesiemeijer
    moderator
    Posted 2 years ago #

    I did change the syntax of the "if" conditional to a specific page id to see if the rest of my function works and it does.

    So if you set a "Page" ID it works?

    Are you trying this inside the loop?

  7. berchman
    Member
    Posted 2 years ago #

    @keesiemeijer
    On the page ID. I wasn't clear.

    I just changed the "if" conditional to something like this:

    if ( is_page ('233')) {
    // do my stuff
    }

    and when viewing page ID 233 "my stuff" appeared.

    When using either solution posted above, the same page ID 233 (which is a grandchild of page ID 8) does not display "my stuff"

    So that would infer that something in the logic is just a touch off.
    But I'm not sure what.

    And yes, this is taking place inside the loop. "My Stuff" is custom content for grandchild pages to display after the page header.

    Let me know if you have any questions.
    Thanks!

  8. keesiemeijer
    moderator
    Posted 2 years ago #

    alchymyth has the better code. But if the $post->ID is not set both our codes will not function.
    try within the loop just for testing:

    <?php
    $ancs = get_ancestors($post->ID, 'page');
    echo '<pre>';
    print_r($ancs);
    echo '</pre>';
    ?>

    And see if that shows you anything.
    Or if the parent Page Id = 233
    try:

    <?php
    $ancs = get_ancestors($post->ID, 'page');
    if( $ancs[1] == 233 ) {
    // do stuff
    echo 'this is a grandchild of page 233';
    }
    ?>
  9. alchymyth
    The Sweeper & Moderator
    Posted 2 years ago #

  10. berchman
    Member
    Posted 2 years ago #

    @keesiemeijer
    When trying the first set of code above

    <?php
    $ancs = get_ancestors($post->ID, 'page');
    echo '<pre>';
    print_r($ancs);
    echo '</pre>';
    ?>

    This is what displayed in the proper position.

    Array
    (
    )

    And when I try the second set of code I get nothing. No result (making sure I have changed the ID to my parent page)

    So I'm not sure if this can be done, can it?

    @alchymyth
    I did see that thread before I posted. I tried some of the code but could not get it to work.
    What part of that code would you use, and how?

    Somewhat frustrating that this is not easier for everyone.
    Let me know if you have any questions.

  11. alchymyth
    The Sweeper & Moderator
    Posted 2 years ago #

    try to see if $post is defined where your code is:

    <?php
    $ancs = get_ancestors($post->ID, 'page');
    echo '<pre>';
    echo $post->ID;//see which post/page ID is set
    print_r($ancs);
    echo '</pre>';
    ?>

    depending on the location of your code, you might need to start with
    global $post;

  12. berchman
    Member
    Posted 2 years ago #

    @alchymyth

    I think your suggestion of global $post; was the ticket.

    Without it I got the empty Array( )

    But with it I get this:

    215Array
    (
        [0] => 35
        [1] => 8
    )

    I think I'm now headed in the right direction.
    Let me know what you think.
    Thanks!

  13. alchymyth
    The Sweeper & Moderator
    Posted 2 years ago #

    looks good - have you tried the conditional codes again?

  14. berchman
    Member
    Posted 2 years ago #

    @alchymyth

    Yes. This looks to produce the desired result:

    global $post;
    $ancs = get_ancestors($post->ID, 'page');
    if( $ancs[1] == 8 ) {
    // do stuff
    echo 'this is a grandchild of page 8';
    }

    Fantastic!! Thanks for the help to you and @ keesiemeijer

    Would setting this up to check for multiples be as easy as adding } else { ...etc??
    I assume it might look like this:

    global $post;
    $ancs = get_ancestors($post->ID, 'page');
    if( $ancs[1] == 8 ) {
    // do stuff
    echo 'this is a grandchild of page 8';
    } else { if( $ancs[1] == 50 ) {
    // do stuff
    echo 'this is a grandchild of page 50';
    }

    Let me know what you think. Not sure if the digit next to $ancs needs +1.
    Thanks!!

  15. John Peden
    Member
    Posted 2 years ago #

    Worked a treat for me! Hats off gentlemen.

Topic Closed

This topic has been closed to new replies.

About this Topic