I'm building a theme that features a section wherein I'd like to have the single most recent post's thumbnail of a single category displayed between the header and blog sections. Something like

<?php if ( has_post_thumbnail()) : ?>
    <?php the_post_thumbnail(); ?>
<?php endif; ?>

would work within the loop, but I'm wondering how I can make this work outside of, or separate from the loop.

Any suggestions?

2 Answers 2


Use get_the_post_thumbnail with a new WP_Query.

 if ( has_post_thumbnail()) {
 $large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'large');
 echo '<a href="' . $large_image_url[0] . '" title="' . the_title_attribute('echo=0') . '" >';
 echo get_the_post_thumbnail($post->ID, 'thumbnail', array('class' => 'aligncentre')); 
 echo '</a>';

you can always do a new wp_query to get the latest post from given category and display its thumbnail etc.. Something like

$new_query = new WP_Query( 'cat=your_category_id&showposts=1' );
if( $new_query->have_posts() ): while( $new_query->have_posts ): $new_query->the_post();



replace your_category_id with a proper value

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.