【问题标题】:Wordpress pagination wp_query->max_num_pages return 0Wordpress 分页 wp_query->max_num_pages 返回 0
【发布时间】:2015-03-07 02:38:19
【问题描述】:

我在 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; ?>

我做错了什么?有什么想法吗?

【问题讨论】:

    标签: wordpress post pagination


    【解决方案1】:
    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;
    }
    

    【讨论】:

      【解决方案2】:

      遇到了同样的问题,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);
        

      【讨论】:

      • wp_count_posts()->publish 为您提供 wordpres 中的总帖子,如果您使用问题(带类别)中的自定义查询,则您的代码无用
      【解决方案3】:

      不太确定它可以帮助某人,但以防万一。我遇到了同样的问题($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');
      

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

      【讨论】:

      • 哇,你是救生员莱拉。浪费了几个小时试图弄清楚为什么分页不起作用。我没有想到怀疑美化的“no_found_rows”。非常感谢!
      • @Darkkz 我的荣幸! :)
      猜你喜欢
      • 1970-01-01
      • 2020-01-30
      • 2015-05-21
      • 2020-03-18
      • 2016-09-29
      • 2023-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多