How do I count the number of child pages of the current page in WordPress?

So I’ve managed to set up a loop inside a loop that counts child pages and applies different markup to them based on whether the total amount of child pages are odd or even, but I’m having trouble finding the right code to count just the pages of the current page.

So here’s my code:

Read More
<?php $the_query = new WP_Query(array( 
    'post_type' => 'page',
    'posts_per_page' => -1,
    'post_parent' => $post->ID));

    $count_posts = wp_count_posts('page');
    $published_posts = $count_posts->publish;

    if($published_posts % 2 == 0) {
        while ( $the_query->have_posts() ) :
            $the_query->the_post(); ?>
            ....some markup 
        <?php endwhile;

    } else {
        while ( $the_query->have_posts() ) :
            $the_query->the_post(); ?>
            ....some different markup
        <?php endwhile;
    }
wp_reset_query(); ?>

I realize that with this line $published_posts = $count_posts->publish; I’m telling it to count all published child pages, I just can’t figure out how to tell it to count all published child pages only of current page.

Any help or insight is, as always, greatly appreciated.
thanks!

Related posts

1 comment

  1. try get_pages

    <?php
    $children = get_pages('child_of='.$post->ID);?>
    if( count( $children ) != 0 ){
    
    }
    else{
    
    }
    ?>
    

Comments are closed.