繁体   English   中英

wp查询应通过wp_reset_query()重置; 是否通过此功能

[英]wp query should be reset by wp_reset_query(); by this function or not

我在wordpress网站中多次使用以下代码

<?php $recent = new WP_Query("showposts=4&cat=186");
      while ($recent->have_posts()) : $recent->the_post();
?>
<a href="<?php the_permalink(); ?>"target="_blank"><b><li>
    <?php the_title(); ?>
    </b></li>
</a><br>
<?php endwhile; ?> 

现在我的问题是,我应该通过wp_reset_query();重置它wp_reset_query(); 如果您的答案是肯定的,那么如何?

在下面尝试此代码结束循环使用WordPress功能

<php wp_reset_postdata();?>

首先,将showpost参数替换为posts_per_page

如果需要访问原始$post的值(例如get_the_ID()等),则需要在每个自定义循环后重置global $post

这行wp_reset_query(); 需要在endwhile时间之后。

PS:您需要重置查询,因为您使用$the_post->the_post()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM