• I’m using this code below on a new site (src is WordPress – see link below). I’ve used it before and it’s worked fine. For some reason it’s not working now for child pages – works for the parent though. The script simply adds a header for the section and all of the pages under it. I feel like I’m missing something obvious. And yes I did assign the children to the correct parent page.

    Sample: http://www.greenmountainsoftware.com/wp/about-us/ View any of the children of About Us.

    function is_tree( $pid ) {      // $pid = The ID of the page we're looking for pages underneath
        global $post;               // load details about this page
    
        if ( is_page($pid) )
            return true;            // we're at the page or at a sub page
    
        $anc = get_post_ancestors( $post->ID );
        foreach ( $anc as $ancestor ) {
            if( is_page() && $ancestor == $pid ) {
                return true;
            }
        }
    
        return false;  // we arn't at the page, and the page is not an ancestor
    }
    
        if (is_tree('2')) {
    ?>
        <div id="artwork"><img src="<?php echo THESIS_CUSTOM_FOLDER; ?>/images/about-us-head.jpg" width="980" height="89" alt="About Green Mountain Software" /><img src="<?php echo THESIS_CUSTOM_FOLDER; ?>/images/fadestrip.jpg" width="980" height="18" alt="Green Mountain Software" /></div>
    <?php
        }

    Code Source: http://codex.wordpress.org/Conditional_Tags#Testing_for_sub-Pages

Viewing 6 replies - 1 through 6 (of 6 total)
  • shouldn’t you be using $pid in here as well:
    $anc = get_post_ancestors( $post->ID ); ?

    Thread Starter Gregg

    (@lorax)

    I don’t think so – but I’m not sure. I think that code gets the id for the current page. $PID is the conditional page id that it needs to be compared against.

    you are right,
    and your code seems to be generally correct
    (tested locally, although in a different theme)

    have you checked if the $post->ID is correct at that point?

    Thread Starter Gregg

    (@lorax)

    Thanks for the follow up. I think that’s my next move.

    Thread Starter Gregg

    (@lorax)

    Hm… when I move this code to within the function that evaluates the Page IDs and chooses the appropriate header, it works!

    global $post;               // load details about this page
        $anc = get_post_ancestors( $post->ID );
        foreach ( $anc as $ancestor ) {
            if( is_page() && $ancestor == $pid ) {
                echo "is child<br/>";
            }
        }
    Thread Starter Gregg

    (@lorax)

    So the only part of that code above that seems to matter is this:

    global $post;               // load details about this page
        $anc = get_post_ancestors( $post->ID );
Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Conditional for determing child pages not working’ is closed to new replies.