Wordpress分页wp_查询->;最大页数返回0

Wordpress分页wp_查询->;最大页数返回0,wordpress,post,pagination,Wordpress,Post,Pagination,我在wordpress自定义页面中的分页有问题。我使用模板,这是分页功能: 但始终$GLOBALS['wp_query']->max_num_pages为0(null)。我尝试了很多次来改变它,这就是我现在所做的: $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1; $query_args = array( 'post_type' => 'post', 'category__in' =&g

我在wordpress自定义页面中的分页有问题。我使用模板,这是分页功能: 但始终$GLOBALS['wp_query']->max_num_pages为0(null)。我尝试了很多次来改变它,这就是我现在所做的:

 $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; ?>
$paged=(获取查询变量('paged'))?获取查询变量('paged'):1;
$query\u args=数组(
“post_type”=>“post”,
'category_uuin'=>'4',
“每页帖子数”=>3,
“最大页数”=>5页,
“paged”=>paged美元
);
//创建WP_查询的新实例
$thew\u query=新的WP\u查询($query\u args);
?>

同样的问题是,max_num_页面在页面模板中不起作用,但在归档模板中起作用。我的解决方案是一点PHP:

  • 数一数所有发表的文章
  • 获取在“选项”中设置的每页帖子
  • 绕到最大值

        $published_posts = wp_count_posts()->publish;
        $posts_per_page = get_option('posts_per_page');
        $page_number_max = ceil($published_posts / $posts_per_page);
    

  • 不太确定它是否能帮助某人,但只是以防万一。我也有同样的问题($wp\u query->max\u pages\u number返回0),我因为一些非常愚蠢的事情而被卡住了

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

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

    这有助于提高WP_查询的性能,但“未找到”行禁用分页。确保它不在插件或主题中:)

    wp\u count\u posts()->publish在wordpres中为您提供全部帖子,如果您使用自定义查询,如问题(带有类别)中的代码未使用哇,您是救命稻草。浪费了几个小时试图找出分页不起作用的原因。我没有想到会怀疑那些被美化的“没有发现”行。非常感谢@我的荣幸!:)
    function no_rows_found_function($query)
    { 
      $query->set('no_found_rows', true); 
    }
    
    add_action('pre_get_posts', 'no_rows_found_function');