WordPress Recent Posts excerpt in sidebar is pulling the page/post excerpt instead of the recent post excerpt

I have a list of Recent Posts in the sidebar of a WordPress blog. The title and author show up properly, but the excerpt that gets shown is the excerpt of the current page/post not the relevant recent post.

The code:

Read More
 <?php $myposts = get_posts('numberposts=10&offset=0');
  foreach($myposts as $post) :?>
  <li><a href="<?php the_permalink(); ?>"><?php the_title();?> <span>by <?php the_author(); ?></span></a> <br /> <?php the_excerpt(); ?></li>
  <?php endforeach; ?>

Any idea why it would be pulling the correct Title/Author, but incorrect excerpt?

Related posts

Leave a Reply

1 comment

  1. <?php $myposts = get_posts('numberposts=10&offset=0');
      foreach($myposts as $post) :
      setup_postdata($post); ?>
      <li><a href="<?php the_permalink(); ?>"><?php the_title();?> <span>by <?php the_author(); ?></span></a> <br /> <?php the_excerpt(); ?></li>
      <?php endforeach;
      wp_reset_query();
    ?>
    

    Postdata isn’t set up. Those functions pull the global values besides $post (e.g. $ID). setup_postdata() sets all the right values. Also, I’d suggest reseting the query after this.