您的页面行为异常,因为您正在使用query_posts 这会破坏主查询。页面上依赖于主查询的任何其他内容在query_posts 跑步Don\'t use query_posts.
$qry = new WP_Query(\'category_name=News and Events, Uncategorized&showposts=5\');
if ($qry->have_posts()) {
while ($qry->have_posts()) {
$qry->the_post();
// your Loop as normal
}
} else {
// no posts found
}
在此循环结束时
global $post 变量将设置为辅助循环中的最后一项,而不是主查询循环中的当前项。您可以使用以下代码对此进行演示:
var_dump($post->post_name); // main Loop
$qry = new WP_Query(array(\'post_type\' => \'page\',\'posts_per_page\' => 1));
while ($qry->have_posts()) {
$qry->the_post();
var_dump($post->post_name); // secondary Loop
}
var_dump($post->post_name); // Still the secondary Loop
因为大多数循环
the_post() 开始时,应在该点重置该变量,因此重置
$post 可能根本没有必要。为了避免这个问题,我原本
wp_reset_query 在循环结束后的一行上,它会进行排序
$post, 但作为
@Rarst points out here, 那太过分了
wp_reset_postdata 会是更好的选择。
参考文献
http://codex.wordpress.org/Function_Reference/query_posts