wp_reset_query() 或 wp_reset_postdata()

rob*_*to 0 wordpress loops

我对此感到困惑,所以我举个例子:

    <ul class="prod">
    <?php   $woop = new WP_Query($wol);
    while ( $woop->have_posts() ) : $woop->the_post(); global $prod; ?>

                <h3><?php the_title(); ?></h3>
<?php woocommerce_template_loop_add_to_cart( $woop->post, $prod );?>    

<?php endwhile; wp_reset_postdata();?>
    </ul>
Run Code Online (Sandbox Code Playgroud)

在这个例子中,我应该使用wp_reset_query()还是wp_reset_postdata()?

我的理解是,我必须wp_reset_postdata()在每次自定义WP_Query()wp_reset_query()每次循环后使用query_posts().

就这么简单吗?

rne*_*ius 5

您永远不必使用wp_reset_query(),它仅用于在使用时将$wp_query数据恢复和全局发布到原始主查询query_posts()(您永远不应该使用)。

相反,你应该只使用wp_reset_postdata()时,要恢复全球$post使用辅助查询循环之后的主查询循环的变量new WP_Query()您在示例中正确使用了它。