Wordpress 分页 wp_query->max_num_pages 返回 0
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27849312/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Wordpress pagination wp_query->max_num_pages return 0
提问by Marcin
I have problem in pagination in wordpress custom page. I using template dazzling, this is pagination function: https://github.com/puikinsh/Dazzling/blob/master/inc/template-tags.phpBut always $GLOBALS['wp_query']->max_num_pages is 0 (null). I try many time to change it, this is what I do now:
我在 wordpress 自定义页面中遇到分页问题。我使用的模板令人眼花缭乱,这是分页功能:https: //github.com/puikinsh/Dazzling/blob/master/inc/template-tags.php但始终 $GLOBALS['wp_query']->max_num_pages 为 0(空) . 我尝试了很多时间来改变它,这就是我现在所做的:
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$query_args = array(
'post_type' => 'post',
'category__in' => '4',
'posts_per_page' => 3,
'max_num_pages' => 5,
'paged' => $paged
);
// create a new instance of WP_Query
$the_query = new WP_Query( $query_args );
?>
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); // run the loop ?>
<article>
<h1><?php echo the_title(); ?></h1>
<div class="excerpt">
<?php the_excerpt(); ?>
</div>
</article>
<?php endwhile; ?>
<?php if ($the_query->max_num_pages > 1) { // check if the max number of pages is greater than 1 ?>
<?php dazzling_paging_nav()); ?>
<?php } ?>
<?php else: ?>
<article>
<h1>Sorry...</h1>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
</article>
<?php endif; ?>
What I do wrong? Any idea?
我做错了什么?任何的想法?
回答by AI.Takeuchi
global $wp_query;
global $page, $numpages, $multipage, $more;
if( is_singular() ) {
$page_key = 'page';
$paged = $page;
$max = $numpages;
} else {
$page_key = 'paged';
$paged = get_query_var( 'paged' ) ? absint( get_query_var( 'paged' ) ) : 1;
$max = $wp_query->max_num_pages;
}
回答by Laila
Not quite sure it can help someone but just in case. I had the same issue ( $wp_query->max_pages_number returning 0) and I got stuck because of something very silly.
不太确定它可以帮助某人,但以防万一。我遇到了同样的问题($wp_query->max_pages_number 返回 0)并且由于一些非常愚蠢的事情而被卡住了。
I realized that I had this code somewhere else in my theme :
我意识到我在我的主题中的其他地方有这个代码:
function no_rows_found_function($query)
{
$query->set('no_found_rows', true);
}
add_action('pre_get_posts', 'no_rows_found_function');
It was be useful to improve the performance of WP_Query but the no_found_rows disables the pagination. Make sure it's not somewhere in a plugin or in your theme :)
提高 WP_Query 的性能很有用,但 no_found_rows 禁用了分页。确保它不在插件或主题中的某个地方:)
回答by Goran Jakovljevic
Had the same problem where max_num_pages wouldnt work in page templates, but would work in archive template. The solution for me was a bit of PHP:
有同样的问题,其中 max_num_pages 不能在页面模板中工作,但可以在存档模板中工作。我的解决方案是一点 PHP:
- Count all published posts
- Get posts per page that is set in options
Round it to max.
$published_posts = wp_count_posts()->publish; $posts_per_page = get_option('posts_per_page'); $page_number_max = ceil($published_posts / $posts_per_page);
- 统计所有已发布的帖子
- 获取在选项中设置的每页帖子
将其四舍五入到最大值。
$published_posts = wp_count_posts()->publish; $posts_per_page = get_option('posts_per_page'); $page_number_max = ceil($published_posts / $posts_per_page);

