Home » Tutorial » How to Paginate Taxonomy Terms with Posts Number

How to Paginate Taxonomy Terms with Posts Number

WordPress Pagination

In a previous post, we discussed how to display a list of taxonomy terms with posts number. What if you have a lot of taxonomy terms?

Suppose you have a lot of custom taxonomy terms, a lot of categories, or a lot of tags. Then you might need to paginate them.

In this tutorial, I will show you how you can create a pagination for taxonomy terms. This works for both hierarchical and non-hierarchical taxonomy. That means you can paginate any taxonomy terms:

  1. Custom taxonomy (hierarchical) terms
  2. Custom taxonomy (non-hierarchical) terms
  3. Categories
  4. Tags

Add the following code to the template where you want to show the paginated list:

$custom_cats = 'quote-writer';
$big = 999999999;
$page = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$per_page = 15;
$number_of_series = count(get_terms($custom_cats));
$offset = ( $page - 1 ) * $per_page;
$term_args = array(
    'number' => $per_page,
    'offset' => $offset
);
$terms = get_terms($custom_cats, $term_args);
if ($terms) {
    foreach ($terms as $term) {
		$term_link = get_term_link( $term);
        echo '<li><a href="'.$term_link.'">'.$term->name . ' (' . $term->count . ')</a></li>';		
    }	
	echo $pageache = paginate_links(array(
    'base' => str_replace($big, '%#%', esc_url(get_pagenum_link($big))),
    'format' => '?paged=%#%',
    'current' => $page,
    'total' => ceil($number_of_series / $per_page)
	));
}

Change ‘quote-writer‘ to your custom taxonomy name. Or you can use ‘category‘ or ‘post_tag‘ here to get the list of paginated categories or tags.

Change the per-page number according to your need.

Bonus: If you want to remove page/1/ from the pagination URL, follow this tutorial: How to Remove page/1/ from WordPress Pagination

Let me know if it works or not.

Read more from Tutorial

Written by:

Morshed Alam
A teacher by profession, a traveler by passion and a netizen by choice.

Have you written on ThoughtMight?Write Today



Leave a Comment

Your email address will not be published. Required fields are marked *