Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Often times, a custom theme for Wordpress will require a dynamic content that reflects a relationship to a top level page of a site.

Is there a Conditional Tag that will check if the current page is a grand-child (or separated by further generations) by ID?

share|improve this question
2  
Answered by yourself less than 5 minutes after you asked the question. Did you already know the answer? I won't downvote because you did answer the question, but I'd still vote to close or delete to remove unnecessary clutter on the site. If you intend to answer your own questions in the future, please wait at least a day to allow for other answers from the community. Asked-and-answered does not fit within the community nature of this site! –  EAMann Dec 2 '10 at 23:07
    
So then, where should I contribute this content so that others can find it easily? I thought sharing information was the point of WordPress Answers. –  PNMG Dec 2 '10 at 23:10
1  
Here's a blog post from Jeff Atwood himself. blog.stackoverflow.com/2011/07/… –  PNMG Jul 1 '11 at 17:25
    
This is the kind of conversation (the merits of answering your own questions) that we should have on Meta ... not in the comments here. –  EAMann Jul 1 '11 at 21:58
add comment

1 Answer

up vote 1 down vote accepted

Turns out there is an excellent function that has been passed around the Wordpress forums is_tree()

function is_tree($pid) {      // $pid = The ID of the page we're looking for pages underneath
    global $post;         // load details about this page

    $anc = get_post_ancestors( $post->ID );
    foreach($anc as $ancestor) {
        if(is_page() && $ancestor == $pid) {
            return true;
        }
    }
    if(is_page()&&(is_page($pid))) 
               return true;   // we're at the page or at a sub page
    else 
               return false;  // we're elsewhere
};

To use it in template, just give it the ID you want to check the current page against, and it will return true if the current page is a descendant.

<?php if(is_tree(12)){echo 'foobar';} ?>

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.