WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to create a simple function to do a "status test". Goal is to test and see if the current page being viewed has any child pages or not. Using this to alter layout to accomodate child pages. The following code seems like it should work, but alas, no dice.

Anyone see what I'm missing?

function is_subpage() {
global $post;                              // load details about this page

if ( is_page() && $post->post_parent ) {   // test to see if the page has a parent
    return true;                                            // return true, confirming there is a parent

} else {                                   // there is no parent so ...
    return false;                          // ... the answer to the question is false


share|improve this question
up vote 2 down vote accepted

Your above function tests whether a page is a child page of some other page, not whether it has children.

You can test for children of the current page like so:

function has_children() {
    global $post;

    $children = get_pages( array( 'child_of' => $post->ID ) );
    if( count( $children ) == 0 ) {
        return false;
    } else {
        return true;

Further reading:

share|improve this answer

Here is version for any post type, in case if you are using custom post type

function has_children($post_ID = null) {
    if ($post_ID === null) {
        global $post;
        $post_ID = $post->ID;
    $query = new WP_Query(array('post_parent' => $post_ID, 'post_type' => 'any'));

    return $query->have_posts();
share|improve this answer

Your Answer


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.