Add is_child and is_ancestor functions

// Check if page is direct child

function is_child($page_id) { 

    global $post; 

    if( is_page() && ($post->post_parent == $page_id) ) {
       return true;
    } else { 
       return false; 
    }

}



// Check if page is an ancestor

function is_ancestor($post_id) {

    global $wp_query;

    $ancestors = $wp_query->post->ancestors;

    if ( in_array($post_id, $ancestors) ) {
        return true;
    } else {
        return false;
    }

}

Add this code to your functions.php file in order to utilize is_child and is_ancestor filters.

Originally sourced by: http://codebyte.dev7studios.com/post/3313452026/wordpress-is-child-and-is-ancestor. This website is unavailable as of August 10, 2017.

Leave a Reply

Posted in php
katherine as a flat graphic icon

About Me

I’m an African / Ojibwe First Nations Web Developer living in Winnipeg, Manitoba.

Visit the Tips and Blog to see what I’m working on.