Displaying Child pages of the current page in post format and their thumbnails (featured image)?

I’m using the following code I got from the WordPress codex:

<?php // Displaying Child pages of the current page in post format
            $mypages = get_pages('child_of='.$post->ID.'&sort_column=post_date&sort_order=desc');
            $count = 0;
            foreach($mypages as $page)
            {
                $content = $page->post_content;
                if(!$content)
                    continue;
                if($count >= 20)
                    break;
                $count++;
                $content = apply_filters('the_content', $content);
            ?>
                <div class="content-block">
                    <h2><a href="<?php echo get_page_link($page->ID) ?>"><?php echo $page->post_title ?></a></h2>
                    <a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_post_thumbnail(); ?></a>
                    <?php echo $content ?>
                </div>
            <?php
            }
        ?>

I tried the following code to get the featured image of the page:

Read More
<a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_post_thumbnail(); ?></a>

and this one too:

$thumbnail = apply_filters('the_post_thumbnail', $thumbnail);

But it doesn’t get the featured image.

Any suggestions?

Related posts

Leave a Reply

2 comments

  1. You’re not inside a regular loop, or at least not one where global post variables are set, so the_post_thumbnail does not have an ID to fetch a thumbnail for..

    Use get_the_post_thumbnai( $page->ID ) instead and it should work fine.

  2. you need to add setup_postdata($page);
    inside your foreach loop and then

    <a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_post_thumbnail(); ?></a>
    

    should work.