WordPress.org

Ready to get started?Download WordPress

Forums

Get_posts, random posts, and thumbnails (7 posts)

  1. frmars
    Member
    Posted 4 years ago #

    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 ?

  2. alchymyth
    The Sweeper & Moderator
    Posted 4 years ago #

    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_the_post_thumbnail

  3. mijk
    Member
    Posted 3 years ago #

    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?

  4. mijk
    Member
    Posted 3 years ago #

    nevermind, I fixed this using the loop.

  5. DanMichel
    Member
    Posted 3 years ago #

    how did you fix this?

  6. alchymyth
    The Sweeper & Moderator
    Posted 3 years ago #

    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

  7. CSSgirl
    Member
    Posted 3 years ago #

    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:

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

Topic Closed

This topic has been closed to new replies.

About this Topic