简体   繁体   中英

Wordpress - pagination for get_terms leading to 404

I have a very long list of categories in my Wordpress site (220 and counting). I want to set a custom page template where I can list all categories in alphabetical order, but being so many, I want to paginate them. Here's my code until now:

$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;

$per_page = 9;
$number_of_series = count( $total_categories );
$offset = $per_page * ( $paged - 1) ;
$totalpages = ceil( $number_of_series / $per_page );

$total_categories_paginated = get_terms( array(
    'taxonomy' => 'category',
    'fields' => 'ids',
    'exclude' => array(608,4003,756),
    'hide_empty' => false,
    'offset' => $offset,
    'number' => $per_page,
) );


function custom_page_navi( $totalpages, $paged, $end_size, $mid_size ) {

    $bignum = 999999999;

    if ( $totalpages <= 1 || $paged > $totalpages ) return;

    return paginate_links( array(
        'base'          => str_replace( $bignum, '%#%', esc_url(get_pagenum_link( $bignum ) ) ),
        'format'        => '',
        'current'       => max( 1, $paged ),
        'total'         => $totalpages,
        'prev_text'     => 'Prev',
        'next_text'     => 'Next',
        'type'          => 'list',
        'show_all'      => false,
        'end_size'      => $end_size,
        'mid_size'      => $mid_size
    ) );
}

foreach ($total_categories_paginated as $single_category) {
    // doing my things with every category
}

printf( '<nav class="pagination">%s</nav>', 
    custom_page_navi( $totalpages, $paged, 3, 3 ) 
);

With this code, I get all the pagination links - and the number of pages is correct - but when I try to go to page 2 (or every other page) I keep getting a 404 error. What am I doing wrong? Thanks! :)

EDIT: even changing the first line to:
$paged = ( get_query_var('page') ) ? get_query_var('page') : 1;
unfortunately doesn't help :(

I guess, you meant to use $paged instead of $page in custom_page_navi definition

Change

function custom_page_navi( $totalpages, $page, $end_size, $mid_size )

to

function custom_page_navi( $totalpages, $paged, $end_size, $mid_size )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM