If anyone can help that'd be great.

I've found a snippet of code that I've used which is pretty much doing what I want it to do, which lists out subpages of the parent, adds a thumbnail if there is one, and added a custom excerpt.

However, the problem is that I can't add individual <?php post_class(); ?> to a div inside the loop, its using the class from the parent and repeating for the subpages.

$child_pages = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_parent = ".$post->ID."    AND post_type = 'page' ORDER BY menu_order", 'OBJECT');    ?>
<?php if ( $child_pages ) : foreach ( $child_pages as $pageChild ) : setup_postdata( $pageChild ); ?>

// Must be inside a loop.
if ( has_post_thumbnail($pageChild->ID) ) {
//  else {
//  echo '<img src="' . get_bloginfo( 'stylesheet_directory' ) . '/images/icon-cropped.png" />';
//  }

<div <?php post_class(); ?>>    
<?php echo get_the_post_thumbnail($pageChild->ID, 'page-thumb-mine'); ?>
<h3><a href="<?php echo get_permalink($pageChild->ID); ?>" rel="bookmark" title="<?php     echo $pageChild->post_title; ?>"><?php echo $pageChild->post_title; ?></a></h3>
<?php echo excerpt(28); ?>
<?php endforeach; endif;

Any help would be appreciated!



1 Answer 1


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:

$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(); ?>>  
        if ( has_post_thumbnail() ) {
        <h3><a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></h3>
        <?php the_excerpt(); ?>
<?php endwhile; ?>


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.




  • @BigBabel You are a star, that works brilliantly, sorry about the messy code, I was trying all sorts of things and couldn't remember the original state. - Yes, I've got a custom excerpt function in my functions file. - Anyway, thank you so much again, really appreciate the time you took to reply.
    – Andy
    Mar 29, 2013 at 16:41
  • 1
    This line of code seems to break this loop: <?php echo excerpt(28); ?> I removed it and the example code worked great.
    – metaColin
    Feb 22, 2017 at 0:00
  • Maybe remove that line from the answer since most users get here via Google and just looking for code that loops child-pages. Dec 23, 2017 at 15:40
  • 1
    @RichardLindhout I had hoped the note I included about that function would be enough to keep people from running into problems. However, considering the age of this question, it definitely wouldn't hurt to make the code example more generic now. Thanks!
    – BigBagel
    Feb 4, 2018 at 23:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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