WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Can I call 5 recent posts' featured images without any text. (3 posts)

  1. aagil001
    Member
    Posted 1 year ago #

    I would like to show the featured images of the five most recent posts on the home page without any text. When a user clicks the image they are taken to the related post. Doesn't sound that hard, huh?

    Images will be uploaded in both orientations, landscape and vertical, but everything I've tried so far wants every "thumbnail" to be the same size, or at least fit in the same size box. I would like to specify a maximum width

    It doesn't have to be the featured image. Perhaps the first image? The big deal is no text.

    Would it be easier to call the images without worrying about their size? They don't need to be re sized, they can be uploaded in the right size.

    Can anyone point me in the right direction, please?

  2. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    to get the latest 5 posts with a featured image:

    <?php $recent = new WP_Query( array( 'posts_per_page' => 5, 'meta_key' => '_thumbnail_id' ) );
    if( $recent->have_posts() ) : while( $recent->have_posts() ) : $recent->the_post();
    echo '<a href="'; the_permalink(); echo '">';
    the_post_thumbnail();
    echo '</a>';
    endwhle; endif; wp_reset_postdata(); ?>

    http://codex.wordpress.org/Class_Reference/WP_Query

    http://codex.wordpress.org/Post_Thumbnails
    http://codex.wordpress.org/Function_Reference/the_post_thumbnail

    if the 'post_thumbnail' settings and parameters don't get the image size right, try to use css.

    if you want to use post's images, there is a versatile plugin 'get the image' which you could try to use alternatively with a query which is not restricted to get only posts with featured image;
    change the first line of the code:

    <?php $recent = new WP_Query( array( 'posts_per_page' => 5 ) );
  3. aagil001
    Member
    Posted 1 year ago #

    Thank you so much for the fast response. I've been dying to get back to my desk to try this out. Since I'm new at this I just pasted that snippet into a few places (one at a time) but did not have any success. Am I using that in index.php or elsewhere?

    Once I get this current project behind me I think I'll have to spend a few days going over the codex more thoroughly. In the past I've always stuck to modifying CSS and adding plugins.

Topic Closed

This topic has been closed to new replies.

About this Topic