我在 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; ?>
知道为什么它会提取正确的标题/作者,但摘录不正确吗?
最佳答案
<?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()
设置所有正确的值。另外,我建议在此之后重置查询。
关于侧边栏中的 WordPress 最近帖子摘录正在拉动页面/帖子摘录,而不是最近的帖子摘录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3515663/