Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question
add comment

2 Answers 2

up vote 3 down vote accepted

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.

share|improve this answer
    
Yeah get_the_post_thumbnail was the answer. Thanks. –  janoChen Feb 17 '11 at 11:43
    
@janoChen - You're welcome.. :) –  t31os Feb 17 '11 at 11:48
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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