WordPress分页wp_query-> max_num_pages返回0

Mar*_*cin 2 wordpress post pagination

我在wordpress自定义页面中的分页有问题。我使用令人眼花 azz乱的模板,这是分页功能: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; ?>
Run Code Online (Sandbox Code Playgroud)

我做错了什么?任何的想法?

Lai*_*ila 9

不太确定它可以帮助某人,但以防万一。我遇到了同样的问题($wp_query->max_pages_number 返回 0)并且由于一些非常愚蠢的事情而被卡住了。

我意识到我在我的主题中的其他地方有这个代码:

function no_rows_found_function($query)
{ 
  $query->set('no_found_rows', true); 
}

add_action('pre_get_posts', 'no_rows_found_function');
Run Code Online (Sandbox Code Playgroud)

提高 WP_Query 的性能很有用,但 no_found_rows 禁用了分页。确保它不在插件或主题中的某个地方:)


小智 5

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;
}
Run Code Online (Sandbox Code Playgroud)


Gor*_*vic 5

有同样的问题,其中 max_num_pages 不能在页面模板中工作,但可以在存档模板中工作。我的解决方案是一点 PHP:

  1. 统计所有已发布的帖子
  2. 获取在选项中设置的每页帖子
  3. 将其四舍五入到最大值。

        $published_posts = wp_count_posts()->publish;
        $posts_per_page = get_option('posts_per_page');
        $page_number_max = ceil($published_posts / $posts_per_page);
    
    Run Code Online (Sandbox Code Playgroud)