0

I am trying to make the thumbnail for each post display on the homepage of a site for a client; although the thumbnails do display, they're not being displayed inside of a specified "thumbnail" div and i cannot figure out why.

You can see the issue yourself here; http://roswell.n8geeks.com. As you can see, the thumbnails are being displayed at the top of the article but should be displayed inside the thumbnail div.

The code

function Last5posts()   {
    $args = array( 'posts_per_page' => 5, 'cat' => '3');                  
    $last_5_posts_query = new WP_Query( $args );
    while($last_5_posts_query->have_posts()) : 
        $last_5_posts_query->the_post();
        $link = get_permalink();
        $title = get_the_title();
        $date = get_the_date();                              

        $content .= '<div class="latest-posts">';
        $content .= '<div class="thumbnail">' .the_post_thumbnail("home-post-thumbnail"). '</div>';
        $content .= '<h3><a href='.$link.' target="_top">'.$title.' / '.$date. '</a></h3>';
        $content .= '<p class="excerpt">' .get_the_excerpt(). '</p>';
        $content .= '</div>';
    endwhile;

return $content;
}

add_shortcode('Last5Posts', 'Last5posts' );

Help is greatly appreciated.

1

the_post_thumbnail() prints the HTML immediately. Use get_the_post_thumbnail() instead to get the HTML into your string.

$content .= '<div class="thumbnail">' 
    . get_the_post_thumbnail( null, "home-post-thumbnail") 
    . '</div>';
  • Thanks, but the thumbnails now no longer display. Any ideas? – Nikki Mather Mar 30 '13 at 2:01
  • @NikkiMather Sorry, had a mistake in my example: the first parameter should be NULL or the post ID. Corrected. – fuxia Mar 30 '13 at 2:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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