【问题标题】:Wordpress wp_reset_query() does not go back to last query?Wordpress wp_reset_query() 不会返回上一个查询?
【发布时间】:2011-06-23 08:36:39
【问题描述】:

这就是我所拥有的:

我正在编辑自定义分类页面。登陆页面时,页面查询会自动设置为返回我所在的自定义分类下的帖子列表。在该页面模板中,我运行此查询帖子:

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

我运行我的循环并且上面完成的查询运行良好。

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

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

      <?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() 正在将查询重置为页面查询,而不是我在第一个代码块中所做的查询。为什么会发生这种情况?如何防止这种情况发生?

亲切的问候

斯科特

【问题讨论】:

    标签: php wordpress loops


    【解决方案1】:

    使用不涉及原始查询的get_posts()!使用您在 codex 页面中找到的示例中的 setup_postdata()。

    <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>
    

    【讨论】:

    • 谢谢。然而,解决这个问题我已经打开了另一个:P
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-11-26
    • 2020-06-15
    • 2014-11-03
    • 2013-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多