这就是我所拥有的:
我正在编辑自定义分类页面。在页面上登陆时,页面查询会自动设置为返回我所在的自定义分类下的帖子列表。在该页面模板中,我运行此查询帖子:
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()正在将查询重置为页面查询,而不是我在第一个代码块中执行的查询。为什么会发生这种情况?如何防止这种情况发生?
亲切的问候
斯科特
答案 0 :(得分:2)
使用未触及原始查询的get_posts()!使用setup_postdata(),如在codex页面中找到的示例。
<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>