WP_Query中分页不起作用()

问题描述:

我的分页不能与WP_Query()一起使用。WP_Query中分页不起作用()

我一共有三个职位。页面1正确显示了所有三个帖子。但第2页显示了相同的三个帖子。实际上,不应该有第2页,因为第1页已经显示了所有三个帖子。

什么可能是错的?


循环中index.php

<?php 
$query = new WP_Query('cat=1'); 
if ($query->have_posts()) : 
    while ($query->have_posts()) : $query->the_post(); 
     the_title(); 
    endwhile; 
endif; 
?> 

<?php my_pagination(); ?> 



分页中functions.php

if (! function_exists('my_pagination')) : 
    function my_pagination() { 
     global $wp_query; 
     $big = 999999999; // need an unlikely integer 
     echo paginate_links(array(
      'base' => str_replace($big, '%#%', esc_url(get_pagenum_link($big))), 
      'format' => '?paged=%#%', 
      'current' => max(1, get_query_var('paged')), 
      'prev_next' => True, 
      'total' => $wp_query->max_num_pages 
     )); 
} 
+1

见我的回答你的其他信息[这里](http://stackoverflow.com/a/25589440/1908141)。这应该解决你所有的问题 – 2014-08-31 06:19:59

您的查询变量在主回路$query,而在你的分页查询变量是$wp_query。尝试使用$query代替$wp_query

因此,例如:

if (! function_exists('my_pagination')) : 
    function my_pagination() { 
     $big = 999999999; // need an unlikely integer 
     echo paginate_links(array(
      'base' => str_replace($big, '%#%', esc_url(get_pagenum_link($big))), 
      'format' => '?paged=%#%', 
      'current' => max(1, get_query_var('paged')), 
      'prev_next' => True, 
      'total' => $query->max_num_pages 
     )); 
} 

注:你的代码似乎缺少一个endif;

+0

它没有工作。分页完全消失。我还添加了更多的帖子来查看第2页是否可以工作,但是没有分页可以转到下一页。还有什么可能是错的?我的主循环存在问题吗?它用于显示特定类别的帖子。分页工作是一个简单的循环。是否有另一种方式来调用特定类别的帖子? – leko 2014-08-31 00:56:19

使用此代码为您的查询尝试:仍然

global $wp_query; 
$paged = get_query_var('paged') ? get_query_var('paged') : 1; 
$wp_query = new WP_Query('cat=1&paged=' . $paged); 
if ($wp_query->have_posts()) : 
    while ($wp_query->have_posts()) : $wp_query->the_post(); 
     the_title(); 
    endwhile; 
endif; 
my_pagination(); 

如果它不起作用(根据具体情况可能会发生这种情况),请尝试更换:

$paged = get_query_var('paged') ? get_query_var('paged') : 1; 

$paged = get_query_var('page') ? get_query_var('page') : 1;