WordPress.org

Support

Support » How-To and Troubleshooting » is page grandchild ?

is page grandchild ?

  • 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?

Viewing 14 replies - 1 through 14 (of 14 total)
  • Moderator keesiemeijer

    @keesiemeijer

    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';
    }
    ?>

    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

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

    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!

    Moderator keesiemeijer

    @keesiemeijer

    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?

    @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!

    Moderator keesiemeijer

    @keesiemeijer

    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';
    }
    ?>

    @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.

    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;

    @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!

    looks good – have you tried the conditional codes again?

    @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!!

    Worked a treat for me! Hats off gentlemen.

Viewing 14 replies - 1 through 14 (of 14 total)
  • The topic ‘is page grandchild ?’ is closed to new replies.
Skip to toolbar