Support » How-To and Troubleshooting » Get_posts, random posts, and thumbnails

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 ) : ?>
    <?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


    Forum Moderator

    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:

    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(', ')); ?>
    <?php endforeach; ?>

    but the post thumbnail’s doesn’t display :/

    Can someone help?

    nevermind, I fixed this using the loop.

    how did you fix this?



    Forum Moderator

    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(); ?>


    Did anyone figure this out? I was having a similar problem using query_posts – but nothing was showing up for the post_thumbnail – someone suggested using get_posts, which I tried, but still no thumbnail:

     $lastposts = get_posts('category_name=todays-menu', 'numberposts=1');
     foreach($lastposts as $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>
     <?php endforeach; ?>
Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Get_posts, random posts, and thumbnails’ is closed to new replies.
Skip to toolbar