WordPress:不同的WP_Query用法:在哪种情况下正确? [英] WordPress: different WP_Query usage: Which is correct, in which context?

查看:60
本文介绍了WordPress:不同的WP_Query用法:在哪种情况下正确?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用两种不同的方法来创建自定义WordPress循环,这两种方法都涉及创建 WP_Query 对象的新实例.我通常在单个页面上有多个循环.

I have been creating custom WordPress loops with two different approaches, both which involve creating a new instance of the WP_Query object. I typically have multiple loops on a single page.

我不明白这两种方法有何不同,以及哪种方法才是正确的背景.

I do not understand how these two approaches differ, and which is the correct context to use each.

方法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 时,我看到第二种方法的错误,例如:

Both appear to yield the same results, however when I turn on WP_DEBUG, I see errors with the second approach such as:

注意:is_singular被错误地调用.条件查询标签在运行查询之前不起作用.

Notice: is_singular was called incorrectly. Conditional query tags do not work before the query is run.

我的问题是这些:

  • 何时应使用 $ original_query = $ wp_query; 方法?
  • 存储和还原 $ wp_query 有什么意义?
  • 为什么在我使用它时会返回错误消息?

推荐答案

某些核心功能-例如一些分页功能(

Some core functions-- some pagination ones, for example (here is an example where this seems to have been a factor)-- assume the $wp_query value. If that isn't set those functions don't work. You can cheat around this by saving the original $wp_query, running the custom loop, and putting $wp_query back. I would argue that this is rarely the best way to do it, and that if you do think you need to do this chances are you should be using pre_get_posts, or other filters, to alter the main query itself instead of making a new query. I won't swear that there absolutely are no cases where this would be appropriate, but I do think there is usually a better way.

您尝试执行此操作时,您的代码不会触发任何通知,但如果您这样做,则将:

Your code does not trigger any notices when I try it but if you did something like this it would:

$original_query = $wp_query;
$wp_query = null;
if (is_single()) {
  echo 'single post';
}
$wp_query = new WP_Query( array('posts_per_page' => 5) );

is_single , is_archive 等功能依赖于尚未设置的全局 $ wp_query 对象.我希望在这种情况下,您会看到通知.

Function like is_single, is_archive, etc. rely on the global $wp_query object, which has been unset. I expect it is in cases like that where you see the notices.

您需要参考的大多数内容都在 http://core.trac.wordpress.org/browser/trunk/wp-includes/query.php

Most everything you need for reference is in http://core.trac.wordpress.org/browser/trunk/wp-includes/query.php and http://core.trac.wordpress.org/browser/trunk/wp-includes/post-template.php

这篇关于WordPress:不同的WP_Query用法:在哪种情况下正确?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆