WordPress 类别分页错误计数

问题描述 投票:0回答:1

我在计算分页的子类别时遇到一些麻烦。无论我为

$cats_per_page
添加什么数字,我总是会看到 10 个页面按钮(包含内容的按钮加上第 10 页之前的空白页面)。

目标是显示第一个类别的所有子类别,并根据我有多少子类别(所以不是帖子)进行分页。只有分页计数部分不起作用,因为它显示的页面比应有的多。我的猜测是问题出在

$cat_count
但我不知道出了什么问题。如有任何帮助,我们将不胜感激。

编辑:添加了一种更简单的方法来计算所有子类别。

$cat_count
$max_num_pages
都给出了正确的值,但分页仍然显示额外的数字(空页)。

    $taxonomy   = 'category';
    
    //count all subcategories of category ID 1
        $argscnt = array('parent' => 1);
        $cat_count = count(get_categories( $argscnt ));
        
    //How many subcategories per page                   
        $cats_per_page = 8;
    
    //get the total number of pages
        $max_num_pages = ceil( $cat_count/$cats_per_page );
        $current_page = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1; // Change to 'page' for static front page
        $offset       = ( $cats_per_page * $current_page ) - $cats_per_page;
        
$cats        = get_terms(
          [
            'taxonomy' => $taxonomy,
             'order'    => 'DESC',
             'child_of'=>1,
             'orderby'    => 'count',
             'number'   => $cats_per_page,
             'offset'   => $offset
          ]
        );
                
                foreach ($cats as $cat) {
                //THE SUBCAT CONTENT
                }
            
            //The pagination
            
             the_posts_pagination( array(
                'mid_size'  => 2,
                'prev_text' => __( 'Back', 'textdomain' ),
                'next_text' => __( 'Onward', 'textdomain' ),
            ) );
php wordpress pagination
1个回答
0
投票

我通过在分页本身中添加这一行

'total' => ceil( $cat_count/$cats_per_page )
找到了解决方案。所以不需要
$max_num_pages
如我上面的代码所示。希望这对某人有帮助。

the_posts_pagination( array(
   'mid_size'  => 2,
    'prev_text' => __( 'Back', 'textdomain' ),
    'next_text' => __( 'Onward', 'textdomain' ),
    'total' => ceil( $cat_count/$cats_per_page ),
 ) );

            
            
© www.soinside.com 2019 - 2024. All rights reserved.