Title: Get_posts, random posts, and thumbnails
Last modified: August 19, 2016

---

# Get_posts, random posts, and thumbnails

 *  [frmars](https://wordpress.org/support/users/frmars/)
 * (@frmars)
 * [16 years, 3 months ago](https://wordpress.org/support/topic/get_posts-random-posts-and-thumbnails/)
 * Hello again
 * At the top of my single pages, a narrow horizontal template displays 5 random
   links.
 *     ```
       <?php global $post;
       $rand_posts = get_posts('numberposts=5&orderby=rand');
       foreach( $rand_posts as $post ) : ?>
   
       [DISPLAY]
   
       <?php endforeach; ?>
       ```
   
 * **Works fine BUT **: I need to display the title AND the thumbnail of the post.
 * • I do get five random titles and links.
 * • But** the_post_thumbail()** returns the thumbnail for the single page I am 
   on _(remember, we are on my single pages)_. It obviously draws info from the 
   query, not from the “get_posts”. Result : I have the five same pictures at the
   top of my page (and in the main post as well).
 * – What is the good solution ?
    – What does GET_POSTS do that I don’t understand?

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

 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [16 years, 3 months ago](https://wordpress.org/support/topic/get_posts-random-posts-and-thumbnails/#post-1392545)
 * you can get the post id, for instance;
 *     ```
       <?php echo $post->ID; ?>
       ```
   
 * and with ‘get_the_post_thumbnail();’ and the id you shold be able to get your
   thumbnail:
    [http://codex.wordpress.org/Template_Tags/get_posts](http://codex.wordpress.org/Template_Tags/get_posts)
   [http://codex.wordpress.org/Template_Tags/get_the_post_thumbnail](http://codex.wordpress.org/Template_Tags/get_the_post_thumbnail)
 *  [mijk](https://wordpress.org/support/users/mijk/)
 * (@mijk)
 * [15 years, 9 months ago](https://wordpress.org/support/topic/get_posts-random-posts-and-thumbnails/#post-1392829)
 * I have similar issue, my code is:
 *     ```
       <?php $myposts = get_posts('numberposts=2&offset=1');
       foreach($myposts as $post) :?>
       <li class="article">
       <?php $thumbID = get_post_thumbnail_id(); ?>
       <a href="<?php the_permalink() ?>"><img src="<?php echo wp_get_attachment_url($thumbID); ?>" alt="<?php the_title(); ?>" /></a>
       <div class="article-over">
       <h2><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
       <?php the_excerpt(); ?>
       <div class="postmetadata">
       Posted: <?php the_time(__('F jS, Y', 'mytheme')) ?> ˑ 
       <?php comments_popup_link(__('No Comments'), __('1 Comment'), __('% Comments'), '', __('Comments Closed') ); ?><br />
       <?php printf(__('Filled under: %s'), get_the_category_list(', ')); ?>
       </div>
       </div>
       </li>
       <?php endforeach; ?>
       ```
   
 * but the post thumbnail’s doesn’t display :/
 * Can someone help?
 *  [mijk](https://wordpress.org/support/users/mijk/)
 * (@mijk)
 * [15 years, 9 months ago](https://wordpress.org/support/topic/get_posts-random-posts-and-thumbnails/#post-1392830)
 * nevermind, I fixed this using the loop.
 *  [DanMichel](https://wordpress.org/support/users/danmichel/)
 * (@danmichel)
 * [15 years, 4 months ago](https://wordpress.org/support/topic/get_posts-random-posts-and-thumbnails/#post-1392840)
 * how did you fix this?
 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [15 years, 4 months ago](https://wordpress.org/support/topic/get_posts-random-posts-and-thumbnails/#post-1392841)
 * instead of:
 *     ```
       <?php $myposts = get_posts('numberposts=2&offset=1');
       foreach($myposts as $post) :?>
       ```
   
 * he was probably using:
 *     ```
       <?php query_posts('posts_per_page=2&offset=1');
       if(have_posts()) : while(have_posts()) : the_post(); ?>
       ```
   
 * [http://codex.wordpress.org/The_Loop_in_Action](http://codex.wordpress.org/The_Loop_in_Action)
 *  [Lindsey](https://wordpress.org/support/users/lalindsey/)
 * (@lalindsey)
 * [15 years, 4 months ago](https://wordpress.org/support/topic/get_posts-random-posts-and-thumbnails/#post-1392843)
 * Did anyone figure this out? I [was having a similar problem](http://wordpress.org/support/topic/using-query_posts-the_post_thumbnail-doesnt-seem-to-work?replies=12#post-1917114)
   using `query_posts` – but nothing was showing up for the `post_thumbnail` – someone
   suggested using `get_posts`, which I tried, but still no thumbnail:
 *     ```
       <?php
        $lastposts = get_posts('category_name=todays-menu', 'numberposts=1');
        foreach($lastposts as $post) :
           setup_postdata($post);
        ?>
        		<div id="article">
                   	<h2>Today's Menu</h2>
       		 <?php the_post_thumbnail(); ?>
                       <h3><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></h3>
                       <?php the_excerpt(); ?> <a href="<?php the_permalink(); ?>">Read More</a>
               </div>
        <?php endforeach; ?>
   
       </div>
       ```
   

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

The topic ‘Get_posts, random posts, and thumbnails’ is closed to new replies.

## Tags

 * [get_posts](https://wordpress.org/support/topic-tag/get_posts/)
 * [query](https://wordpress.org/support/topic-tag/query/)
 * [thumbnails](https://wordpress.org/support/topic-tag/thumbnails/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 6 replies
 * 5 participants
 * Last reply from: [Lindsey](https://wordpress.org/support/users/lalindsey/)
 * Last activity: [15 years, 4 months ago](https://wordpress.org/support/topic/get_posts-random-posts-and-thumbnails/#post-1392843)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
