Question

I've created a paginated list of terms for a custom taxonomy, but the pagination is not showing correctly. No matter how many posts per page I set, only two pages are output.

So with 6 posts and set to 6 per page, I see two pages, the second one blank. With 6 posts and 2 per page I see two pages, and there is no third page (I've tried typing /page/3/ into the url bar and the page is not found, so it's not just a problem with the pagination buttons but seems to be a problem with the number of pages being output by Wordpress).

Can anyone see why this might be? How can I get the pagination working correctly?

I've added a bounty to this question, if anyone can suggest a way of getting this pagination working correctly.


UPDATE 19/12:

I'm part of the way to solving this. I've discovered part of the issue seems to be partly to do with the Reading settings in WP. I had the reading settings set to 6 per page, which is why no matter what I set in $posts_per_page in the template file, I only got an output of 2 pages.

Now I can get Wordpress to output all the pages and pagination links if I make sure that the reading settings match the number set in the template file, but I always get an extra output from the for loop, resulting in an empty div. This causes an extra page in some instances depending on the number of posts on the last page. I'm also getting no pagination links on the last page.


Code excerpt from archive-prints.php:

$posts_per_page = 6;

$page = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$offset = ( $page - 1 );

$categories = get_terms('prints_cat');

for( $i = $offset * $posts_per_page; $i < ( $offset + 1 ) * $posts_per_page; $i++ ) {
    $category = $categories[$i];
    echo '<div class="cat-preview"><a href="';
    echo get_term_link($category->slug, 'prints_cat');
    echo '"><h2>';
    echo $category->name;
    echo '</h2></a></div>';
}
unset( $category );

custom_page_navi();

And the code for my custom_page_navi() function, from my functions.php file:

function custom_page_navi() {
global $wp_query;
$bignum = 999999999;
if ( $wp_query->max_num_pages <= 1 )
return;

echo '<nav class="pagination">';

    echo paginate_links( array(
        'base'          => str_replace( $bignum, '%#%', esc_url( get_pagenum_link($bignum) ) ),
        'format'        => '',
        'current'       => max( 1, get_query_var('paged') ),
        'total'         => $wp_query->max_num_pages,
        'prev_text'     => 'Prev',
        'next_text'     => 'Next',
        'type'          => 'list',
        'show_all'      => false,
        'end_size'      => 2,
        'mid_size'      => 0
    ) );

echo '</nav>';

}

Was it helpful?

Solution

You could create a custom page template file tpl_list.php with the following code:

<?php
/**
 * Template Name: Paginated list of terms for a custom taxonomy
 *
 */

// Edit:
$taxonomy = 'prints_cat';
$number   = 3; // number of terms to display per page

// Setup:
$page         = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$offset       = ( $page > 0 ) ?  $number * ( $page - 1 ) : 1;
$totalterms   = wp_count_terms( $taxonomy, array( 'hide_empty' => TRUE ) ); 
$totalpages   = ceil( $totalterms / $number );

// Debug:
// printf( 'taxonomy: %s - number: %s - page: %s - offset: %s - totalterms %s - totalpages: %s' , $taxonomy, $number, $page, $offset, $totalterms, $totalpages );

// Here I list all the available paramters to get_terms():
$args = array(
    'orderby'       => 'name', 
    'order'         => 'ASC',
    'hide_empty'    => true, 
    'exclude'       => array(), 
    'exclude_tree'  => array(), 
    'include'       => array(),
    'number'        => $number, 
    'fields'        => 'all', 
    'slug'          => '', 
    'parent'         => '',
    'hierarchical'  => true, 
    'child_of'      => 0, 
    'get'           => '', 
    'name__like'    => '',
    'pad_counts'    => false, 
    'offset'        => $offset, 
    'search'        => '', 
    'cache_domain'  => 'core'
); 

$terms = get_terms( $taxonomy, $args );

foreach ( $terms as $term )
{
    printf( '<div class="cat-preview"><h2><a href="%s">%s</a></h2></div>',
            get_term_link($term->slug, 'country'),
            $term->name,
            $term->name 
        );
}

// Show custom page navigation
printf( '<nav class="pagination">%s</nav>', 
            custom_page_navi( $totalpages, $page, 3, 0 ) 
          );

where

function custom_page_navi( $totalpages, $page, $end_size, $mid_size )
{
    $bignum = 999999999;

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

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

Create a page (for example called prints ) and select the above page template.

Then you can visit:

   example.com/prints/
   example.com/prints/page/2/
   example.com/prints/page/3/

And if you uncomment the debug line, you will get for example:

taxonomy: prints_cat - 
number: 3 - 
page: 2 - 
offset: 3 - 
totalterms 6 - 
totalpages: 2

OTHER TIPS

The value of your

'end_size' => 2, 'mid_size' => 0

is different from the the default values mentioned in codex, can you try changing them to

'end_size' => 1, 'mid_size' => 2

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top