Child Pages Loop

I’m fairly certain the problem is that some template tags rely on the global $post variable. Using setup_postdata() as you are now, will not alter $post. If you replace all the instances of $pageChild with $post, everything should work.

However, I would strongly recommend using the WP_Query class and setting up your post data with ‘the_post()’ instead. Here is the equivalent of your code, but with WP_Query:

<?php
$args = array(
    'post_parent' => $post->ID,
    'post_type' => 'page',
    'orderby' => 'menu_order'
);

$child_query = new WP_Query( $args );
?>

<?php while ( $child_query->have_posts() ) : $child_query->the_post(); ?>

    <div <?php post_class(); ?>>  
        <?php  
        if ( has_post_thumbnail() ) {
            the_post_thumbnail('page-thumb-mine');
        }
        ?>
        <h3><a href="https://wordpress.stackexchange.com/questions/93844/<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></h3>
        <?php the_excerpt(); ?>
    </div>
<?php endwhile; ?>

<?php
wp_reset_postdata();

Note: I cleaned up a few other things in your posted code. Also, I swapped out your custom excerpt() function with the_excerpt() so the example code works for anyone that wants to copy/paste it.

References:

https://codex.wordpress.org/Class_Reference/WP_Query

https://codex.wordpress.org/Function_Reference/setup_postdata

Leave a Comment