WordPress wp_reset_query()是否不返回上一个查询? [英] Wordpress wp_reset_query() does not go back to last query?

查看:83
本文介绍了WordPress wp_reset_query()是否不返回上一个查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这就是我所拥有的:

我正在编辑自定义分类页面.在页面上着陆时,页面查询将自动设置为返回我所使用的自定义分类法下的帖子列表.在该页面模板中,我运行以下查询帖子:

Im editing a custom taxonomy page. On landing on the page the page query is automatically set to return a list of posts under the custom taxonomy I'm on. In that page template I run this query posts:

        query_posts(
            array_merge(
                array( 'post__in' => $_SESSION['lpoc_search_data'], 'orderby' => 'post__in' ),
                $wp_query->query
            )
        );

我运行我的循环,上面已经完成的查询工作正常.

I run my loop and the query Ive done above works all well.

<?php while (have_posts()) : the_post(); ?>
  My Loop
<?php endwhile; ?>

但是在上面的循环中,我进行了另一个查询:

But inside the above loop I do another query:

      <?php $args = array('p' => $officeID, 'post_type' => "offices"); query_posts($args); ?>
      <?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
         //Inside secondary loop
      <?php endwhile; ?>
      <?php wp_reset_query(); ?>

如您所见,我使用wp_reset_query();这样上面的循环将返回其原始状态.否则,您会认为.但是发生的是wp_reset_query()将查询重置为页面查询,而不是我在第一个代码块中所做的查询.为什么会发生这种情况,如何防止这种情况发生?

As you can see I use wp_reset_query(); so that the loop above is returned to its original state. Or so you would think. But what is happening is that wp_reset_query() is resetting the query to the page query and not the query I did in the first code block. Why is this happening and how can I prevent this from happening?

亲切的问候

斯科特

推荐答案

使用 get_posts( )不会影响原始查询!如在法典页面上的示例中一样,使用setup_postdata().

Use get_posts() that is not touching the original query! Use setup_postdata() as in the example that you find in the codex page.

<ul>
<?php
  global $post;
  $tmp_post = $post;
  $args = array( 'numberposts' => 5, 'offset'=> 1, 'category' => 1 );
  $myposts = get_posts( $args );
  foreach( $myposts as $post ) : setup_postdata($post); ?>
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
  <?php endforeach; ?>
<?php $post = $tmp_post; ?>
</ul>

这篇关于WordPress wp_reset_query()是否不返回上一个查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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