tags:

views:

31

answers:

1

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:

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

+2  A: 
<?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.

John P Bloch
Awesome, thanks!
christina