Title: Using post thumbnails outside the loop
Last modified: August 19, 2016

---

# Using post thumbnails outside the loop

 *  [rubberband](https://wordpress.org/support/users/rubberband/)
 * (@rubberband)
 * [16 years ago](https://wordpress.org/support/topic/using-post-thumbnails-outside-the-loop/)
 * Hey everyone. What I’m trying to do is use the new post thumbnails feature in
   conjunction with the WordPress.com Stats plugin. I’m using this code to grab 
   the most popular posts and display them on one of my pages:
 *     ```
       <h4>Most Popular</h4>
       <ul>
       <?php foreach($top_posts = stats_get_csv('postviews', "days=7") as $post): if(!get_post($post['post_id'])) continue; ?>
       	<li><a href="<?php echo $post['post_permalink']; ?>"><?php echo $post['post_title']; ?></a></li>
   
       <?php endforeach; ?>
       ```
   
 * That is working and displaying the posts perfectly fine. However, what I’m trying
   to do is represent each post by a thumbnail instead of the post title. I’ve tried
   squeezing <?php the_post_thumbnail(); ?> in there, but it doesn’t seem to be 
   working.
 * Is this even possible to do? Any help is appreciated.

Viewing 7 replies - 1 through 7 (of 7 total)

 *  Thread Starter [rubberband](https://wordpress.org/support/users/rubberband/)
 * (@rubberband)
 * [16 years ago](https://wordpress.org/support/topic/using-post-thumbnails-outside-the-loop/#post-1473715)
 * If anyone could help me set up a custom WP query that would basically do the 
   same thing using WordPress.com stats, that would be even better.
 *  [esmi](https://wordpress.org/support/users/esmi/)
 * (@esmi)
 * [16 years ago](https://wordpress.org/support/topic/using-post-thumbnails-outside-the-loop/#post-1473720)
 * [http://codex.wordpress.org/Function_Reference/get_posts#Access_all_post_data](http://codex.wordpress.org/Function_Reference/get_posts#Access_all_post_data)
 *  [Mark / t31os](https://wordpress.org/support/users/t31os_/)
 * (@t31os_)
 * [16 years ago](https://wordpress.org/support/topic/using-post-thumbnails-outside-the-loop/#post-1473724)
 * You’ll need to pass the post id into the thumbnail call.
 *     ```
       <?php the_post_thumbnail( (int) $post['post_id'] ); ?>
       ```
   
 *  Thread Starter [rubberband](https://wordpress.org/support/users/rubberband/)
 * (@rubberband)
 * [16 years ago](https://wordpress.org/support/topic/using-post-thumbnails-outside-the-loop/#post-1473741)
 * Could either of you explain this in a little more detail? Sorry, I don’t really
   understand PHP too well.
 * t31os_, is this what you were talking about?
 *     ```
       <h4>Most Popular</h4>
   
       <?php foreach($top_posts = stats_get_csv('postviews', "days=7") as $post): if(!get_post($post['post_id'])) continue; ?>
       	<a href="<?php echo $post['post_permalink']; ?>"><?php the_post_thumbnail( (int) $post['post_id'] ); ?></a>
   
       <?php endforeach; ?>
       ```
   
 *  [Mark / t31os](https://wordpress.org/support/users/t31os_/)
 * (@t31os_)
 * [16 years ago](https://wordpress.org/support/topic/using-post-thumbnails-outside-the-loop/#post-1473791)
 * Yes, that’s what i meant, but it’s not possible anyway, having checked…
 * You’ll need to use this instead..
 *     ```
       <?php echo get_the_post_thumbnail( (int) $post['post_id'] ); ?>
       ```
   
 *  [areck](https://wordpress.org/support/users/areck/)
 * (@areck)
 * [15 years, 2 months ago](https://wordpress.org/support/topic/using-post-thumbnails-outside-the-loop/#post-1474033)
 * Is there any way to display all posts thumbnails outside the loop? I need to 
   create some kind of a gallery with posts thumbnails so i need to display them
   all together
 *  [areck](https://wordpress.org/support/users/areck/)
 * (@areck)
 * [15 years, 2 months ago](https://wordpress.org/support/topic/using-post-thumbnails-outside-the-loop/#post-1474034)
 * I managed to display all attachments via code
 *     ```
       <?php
       $args = array( 'post_type' => 'attachment', 'numberposts' => -1, 'post_status' => null, 'post_parent' => null );
       $attachments = get_posts( $args );
       if ($attachments) {
       	foreach ( $attachments as $post ) {
       		setup_postdata($post);
       		the_title();
       		the_attachment_link($post->ID, false);
       		the_excerpt();
       	}
       }
       ?>
       ```
   
 * That displayed all the attachments including featured images. So how to exclude
   now any other types of attachments and leave only the featured images?

Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘Using post thumbnails outside the loop’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 7 replies
 * 4 participants
 * Last reply from: [areck](https://wordpress.org/support/users/areck/)
 * Last activity: [15 years, 2 months ago](https://wordpress.org/support/topic/using-post-thumbnails-outside-the-loop/#post-1474034)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
