2

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:

<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?

2 Answers 2

3

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
  • Yeah get_the_post_thumbnail was the answer. Thanks.
    – wyc
    Commented Feb 17, 2011 at 11:43
  • @janoChen - You're welcome.. :)
    – t31os
    Commented Feb 17, 2011 at 11:48
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.

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.