It is very interesting and exciting to work in wordpress and this time I am going to explain to use paginate taxonomy in WordPress.
Usually, we use pagination with post and custom post types.Its easy to use pagination with post when you are using while loop and have_post function.
But to use same pagination function,its not quite simple to use with taxonomy or custom taxonomy. because you will bel get ‘404 – Not Found’ errors when trying to link url with < taxonomy_url >/page/2
Read about: To import terms from csv in wordpress
So, Let’s understand code which will be work with taxonomy loop:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | //get_query_var to get page id from url $page = ( get_query_var('paged') ) ? get_query_var('paged') : 1; // number of tags to show per-page $per_page = 10; //count total number of terms related to passed taxonomy $number_of_series = count(get_terms('categories')); $offset = ( $page - 1 ) * $per_page; $term_args = array( 'number' => $per_page, 'offset' => $offset ); $terms = get_terms('categories', $term_args); if ($terms) { foreach ($terms as $term) { //your code goes here } //foreach ends // if (function_exists("pagination")) { $big = 999999999; //your function for pagination or plugin you added for pagination home_pagination(); echo paginate_links(array( 'base' => str_replace($big, '%#%', esc_url(get_pagenum_link($big))), 'format' => '?paged=%#%', 'current' => $paged, 'total' => ceil($number_of_series / $per_page) // like 10 items per page )); } } //if ends |
That’s it.It’s a wrap!.This concludes today’s tutorial! I’m hoping that you’ve found this tutorial useful.
Read: Pagination when getting posts from category in WordPress
Don’t hesitate to use this code in your WordPress projects and post your comments if you need help. As always, thanks for reading. Don’t Forget to Follow us on Twitter or Subscribe us to Get the Latest Updates.
Comments (5)