1

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?

4
  • 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, 2010 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, 2010 at 23:10
  • 1
    Here's a blog post from Jeff Atwood himself. blog.stackoverflow.com/2011/07/…
    – PNMG
    Jul 1, 2011 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, 2011 at 21:58

1 Answer 1

1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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