在侧边栏中的Wordpress最新帖子摘录是拉取页面/帖子摘录而不是最近的帖子摘录

时间:2010-08-18 18:58:32

标签: wordpress

我在Wordpress博客的侧边栏中列出了最近的帖子。标题和作者正确显示,但显示的摘录是当前页面/帖子的摘录,而不是最近的相关帖子。

代码:

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

知道为什么它会提取正确的标题/作者,但不正确的摘录?

1 个答案:

答案 0 :(得分:4)

<?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。除了$ post(例如$ ID)之外,这些函数还会提取全局值。 setup_postdata()设置所有正确的值。此外,我建议在此之后重新设置查询。