wordpress循环返回循环外的全部内容



我使用一个基本的wordpress循环来显示3篇博客文章+分页,使用query_posts来循环这些-附加代码。循环和分页工作得很好,但它显示了一个奇怪的故障:整个页面的内容(所有3篇文章(也被循环和显示。完整内容显示在分页之后和endwhile结束标记之后,即所有代码的末尾。

我的怀疑是,这个循环不知何故没有关闭,但我知道要检查的任何关闭标签都已经存在。我的php还处于初级入门阶段,主要集中在标准的wordpress函数上,所以任何关于如何关闭它的建议都是受欢迎的。

注意:由于分页,循环使用query_posts而不是new WP_Query,这在使用任何new WP_Query循环时都不起作用。我想去掉额外的内容,但保持基本循环不变,这样分页仍然有效。

最后一点:我已经验证了额外的内容是由这个循环引起的,因为删除循环也删除了内容。

<article class="row">
<?php query_posts(array('posts_per_page' => 3, 'post_type' => 'post', 'paged' => get_query_var('page')));
if (have_posts()) : while (have_posts()) : the_post(); ?>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail(); ?>
<h3><?php the_title(); ?></h3>
<?php the_excerpt(); ?>
<span class="inline-button">Read more <i class="fas fa-chevron-right"></i></span>
</a>
<?php endwhile; ?>
<?php endif;?>
<?php wp_reset_postdata(); ?>
</article>
<article class="row">
<?php   the_posts_pagination(); ?>
</article>

<!------ in html, all extra generated content will appear here below the above article --->

已解决!

我找到了答案,并简单地关闭了它,最后添加了这个:

<?php wp_reset_query() ;?>

相关内容

最新更新