WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How to display most recent post with thumbnail and excerpt (3 posts)

  1. pinksharpii
    Member
    Posted 4 months ago #

    For my website I'm trying to get the most recent post summary to be displayed in the footer across all pages.
    I couldn't get the_excerpt() to work at all with

    <?php
      $args = array( 'numberposts' => '1' );
      $recent_posts = wp_get_recent_posts( $args );
      foreach( $recent_posts as $recent ){?>
          <div class="summary"><?php echo '<a href="' . get_permalink($recent["ID"]) . '" title="Read More '.$recent["post_title"].'" >' . substr($recent["post_content"], 0 , 150) .'</a> '; ?></div>
    <?php echo the_post_thumbnail($recent->ID, 'thumbnail'); ?>
      <?php } ?>

    hence the substr with post_content.
    But what's really tripping me up is the thumbnail.
    I want this displayed on all pages. On the home page and other pages which do not, of course, have a thumbnail image, nothing displays. But on pages (like specific product pages for our shop) the product thumbnail is displayed. Obviously this is not what I want. Nor does it make sense. I'm passing it $recent->ID so it should always display the thumbnail that corresponds with the most recent post. The text and title and perma of the recent post work just fine.
    What do I need to do to display the recent post's thumbnail?

  2. The Grey Parrots
    Member
    Posted 4 months ago #

    Kindly try like this

    <?php
     $postslist = get_posts('numberposts=3&order=DESC&orderby=date');
     foreach ($postslist as $post) :
        setup_postdata($post);
     ?>
     <div class="entry">
     <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
     <?php the_time(get_option('date_format')) ?>
     <?php the_excerpt(); ?>
     </div>
     <?php endforeach; ?>
  3. pinksharpii
    Member
    Posted 4 months ago #

    omgosh, yes, this worked. So simply too! Thanks so much!!

Reply

You must log in to post.

About this Topic