我一直在使用两种不同的方法创建自定义 WordPress 循环,这两种方法都涉及创建WP_Query
对象的新实例。我通常在一个页面上有多个循环。
我不明白这两种方法有何不同,以及使用每种方法的正确上下文。
方法1:http ://codex.wordpress.org/Class_Reference/WP_Query
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();
// output
endwhile;
endif;
wp_reset_postdata();
方法2:http ://codex.wordpress.org/Function_Reference/wp_reset_postdata
$original_query = $wp_query;
$wp_query = null;
$wp_query = new WP_Query( $args );
if ( have_posts() ) :
while ( have_posts() ) : the_post();
// output
endwhile;
endif;
$wp_query = null;
$wp_query = $original_query;
wp_reset_postdata();
两者似乎产生相同的结果,但是当我打开时WP_DEBUG
,我看到第二种方法的错误,例如:
注意: is_singular 调用不正确。条件查询标签在查询运行之前不起作用。
我的问题是:
- 我什么时候应该使用这种
$original_query = $wp_query;
方法? - 存储和恢复的相关性是什么
$wp_query
? - 为什么我使用时返回错误信息?