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 开发者_StackOverflowthe 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?
<?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.
精彩评论