我正在使用以下代码来检索帖子:

<?php
$featuredPosts = new WP_Query();
$featuredPosts->query('showposts=5&cat=3');

while ($featuredPosts->have_posts()) : $featuredPosts->the_post(); ?>

    <h1><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h1>
    <div class="meta">
        By <?php the_author() ?>
    </div>
    <div class="storycontent">
        <?php the_excerpt(); ?>
    </div>

<?php endwhile; ?>


我需要使用wp_reset_query()吗?如果可以,应该放在哪里?

评论

相关:什么时候应该使用WP_Query vs query_posts()vs get_posts()?

如果您依赖页面中其他地方的主查询对象,则可以!您应该调用它,以确保在遍历自定义查询之前已完成主查询对象中包含的数据。当您调用the_post()方法(即$ my_custom_query-> the_post())时,您将重新填充主查询所查看的post变量,当您调用它时,reset将使用先前的数据重新填充这些变量。在自定义查询之后,最好使用重置。

#1 楼

@janoChen大家好:

简单答案:没有。

以下是WordPRess v3.0.4中wp_reset_query()/wp-includes/query.php函数的PHP代码以及随后调用的函数。您可以看到它主要是关于修改全局变量的。

使用new WP_Query($args)时,您将把值的返回值分配给局部变量,因此,除非您做的事情太复杂以至于您不知道该问题的答案,否则,您不会不需要致电wp_reset_query()

function wp_reset_query() {
  unset($GLOBALS['wp_query']);
  $GLOBALS['wp_query'] =& $GLOBALS['wp_the_query'];
  wp_reset_postdata();
}

function wp_reset_postdata() {
  global $wp_query;
  if ( !empty($wp_query->post) ) {
    $GLOBALS['post'] = $wp_query->post;
    setup_postdata($wp_query->post);
  }
}

function setup_postdata($post) {
  global $id, $authordata, $day, $currentmonth, $page, $pages, $multipage, $more, $numpages;

  $id = (int) $post->ID;

  $authordata = get_userdata($post->post_author);

  $day = mysql2date('d.m.y', $post->post_date, false);
  $currentmonth = mysql2date('m', $post->post_date, false);
  $numpages = 1;
  $page = get_query_var('page');
  if ( !$page )
    $page = 1;
  if ( is_single() || is_page() || is_feed() )
    $more = 1;
  $content = $post->post_content;
  if ( strpos( $content, '<!--nextpage-->' ) ) {
    if ( $page > 1 )
      $more = 1;
    $multipage = 1;
    $content = str_replace("\n<!--nextpage-->\n", '<!--nextpage-->', $content);
    $content = str_replace("\n<!--nextpage-->", '<!--nextpage-->', $content);
    $content = str_replace("<!--nextpage-->\n", '<!--nextpage-->', $content);
    $pages = explode('<!--nextpage-->', $content);
    $numpages = count($pages);
  } else {
    $pages = array( $post->post_content );
    $multipage = 0;
  }

  do_action_ref_array('the_post', array(&$post));

  return true;
}




-麦克

评论


@janoChen-嘿。他肯定是最近一直在推我,这是肯定的!我猜就像他们说的那样,竞争可以提高品种(但可以肯定的是,我无法完成任何其他富有成效的工作!'-)

– MikeSchinkel
2011年1月29日7:20

仅针对其他阅读此内容的人,因为这仍然是可接受的答案(@Rarst的答案应被接受)。由于OP在他的代码中使用the_post(),因此最佳实践表明他必须使用wp_reset_postdata()。 wp_reset_query()调用wp_reset_postdata(),这样就可以了,尽管wp_reset_query()所做的另一件事-重置$ wp_query全局变量-是不必要的,但在这种情况下没有害处。所以答案是肯定的

–汤姆·俄格(Tom Auger)
13年7月22日在19:22

#2 楼

WP_Query本身不是必需的,但是如果您使用任何相关的函数/方法(例如the_post()setup_postdata())来用数据填充全局变量,则这是必要的(或至少是一件好事)。

基本上创建新的WP_Query对象只是数据检索,但使用它运行活动循环并使模板标签可访问数据确实会修改环境,因此最好重置所有内容。

总体-调用它对性能没有任何意义,因此始终调用它比确定是否应该或忘记它以及使某些东西神秘地损坏要容易得多。 wp_reset_postdata()函数似乎是更合适的选择。 wp_reset_query()重置全局$wp_query(自定义WP_Query对象不影响)和$post(可能与上面相同)变量。 wp_reset_postdata()仅还原$post,应该足够了。

#3 楼

否。如果您实例化自己的WP_Query对象,则它与您将要执行的操作有关。但是,如果您篡改了global $wp_query变量,那么,全局命名空间中的您会影响同时使用该变量的任何人的脚本。而且,如果您要更改其中的数据,则还必须在使用完之后重置它。

#4 楼

如果您使用这样的自定义查询

$cat = new WP_query(); 
$cat->query("cat=19,20,-23&showposts=5&orderby=rand"); 
while ($cat->have_posts()) : $cat->the_post(); 
  $data = get_post_meta( $post->ID, 'key', true );
$img_arrays []= $data['productimage']; 
$lnk_arrays[] =get_permalink($post_ID); 
endwhile; 
wp_reset_query(); 


那么您就不会遇到问题。否则,如果在同一页面上还有另一个循环,则势必会得到一些意外的结果。我没有在上面的代码中使用过wp_reset_query()(将其放置在header.php文件中。然后,当我进入single.php时,大多数时候我都得到了其他类别的详细信息页面,这令人沮丧。后来,我意识到我忘了重设顶部的查询。很快,它开始像一个超级按钮一样起作用。