0

I have a website (http://sfa.uconn.edu/) that displays left menu with a list of child pages on any parent page (and its children), like this:

enter image description here

However, I would like to hide this functionality from top level pages with no children, because it just displays the page title twice, like this:

enter image description here

Does anyone know a way to test if (1) the current page is top-level and (2) the page has no children.

0

To expand upon my comment:

Top-level page? (q.1)

global $post;
$x = get_ancestors( $post->ID, 'page' );
if( ! $x ) {
    // there are no ancestors, therefore this is a top-level page
}

Childless page? (q.2)

global $post;
$args = array(
    'post_parent' => $post->ID,
    'post_type' => 'page',
);
$x = get_children( $args );
if( ! $x ) {
    // there are no children
}

References

NB -- this assumes that you're after WordPress's pages. If you want a different post type, replace page in the code samples with your chose post type's name.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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