WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question

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>';
share|improve this answer

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

share|improve this answer

Your Answer


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.