So I have a page where I want to list posts of specific category and numbered pagination based on that category. Listing posts was not a problem as I'm using Divi which does it easily. But pagination is not going along.

Assuming following scenario:

  • Category name = Events
  • No. of post in Events catyegory = 6
  • Max post per page = 2

Expected output:

1 2 3 Next

Current output:

1 2 3 .. 6 Next

Basically it's showing all numbers i.e number of pagination blocks is equal to no. of posts in that category.

Code in fucntions.php for pagination

function myPagination($args = '')
    if (!isset($args['category'])) {
    $cat_id = get_cat_ID($args['category']);
    $mycats = get_categories("include=$cat_id");
    $total = $mycats[0]->category_count;
    /*only bother with the rest if we have more than 1 page!*/
    if ($total > 1) {
        /*get the current page*/
        if (!$current_page = get_query_var('paged')) {
            $current_page = 1;
        /*structure of "format" depends on whether we're using pretty permalinks*/
        if (get_option('permalink_structure')) {
            $format = '&paged=%#%';
        } else {
            $format = 'page/%#%/';
        $pagination = paginate_links(array(
            'base'     => get_pagenum_link(1) . '%_%',
            'format'   => $format,
            'current'  => $current_page,
            'total'    => $total,
            'mid_size' => 3,
            'type'     => 'list',
            'add_args' => $query_args
        return $pagination;

The issue I can think of is it's not taking max number of posts to show to create the page gaps.

Any help would be much appreciated.

Thank you,

  • Why are you buffering output on a section of code that produces no output? Aug 7, 2016 at 14:43
  • @AndyMacaulay-Brook Previously I was echoing $pagination instead of returning. As a result function output was always on top content despite where I put the shortcode. It was for that. I'll remove that. Anything you could help for the main issue please?
    – Prajwol
    Aug 7, 2016 at 14:48

1 Answer 1


You did enter total number of posts/events as total page count. you should have passed total number of pages instead of total pages as "total" args. So to pass as total pages, you should calculate like TOTAL_POSTS/PER_PAGE.. so if you have 6 posts and you have 2 posts per page then it would be 6/2=3 pages.. so the "total" value should have been 3.. if you have 7 posts.. then it would be 7/2=3.5=4 pages.. PHP ceil function does that trick to round off to highest value.

So part of your code should be


Where $my_per_page would be your Per Page variable.

I hope this fixes your issue

  • Perfect. That's exactly what I was missing. Thank you. One more thing. How do I get the value for posts per page if they are set different in each page. I've tried $wp_query->post_count but it returns 1 as it's returning page itself.
    – Prajwol
    Aug 7, 2016 at 15:19
  • Well.. normally, if you are doing custom page of posts or custom post types.. then you will pass the max_num_pages object value from your custom WP_Query object.. like.. $my_query=new WP_Query(array(...)); $totalPages=$my_query->max_num_pages; that object would calculate what number of pages you will have without having to calcuate without using ceil or any other similar procedures. Aug 7, 2016 at 16:17
  • I'm using Divi theme and have no clue what object name are they using. I'll simply add one more argument to pass the number of posts required. This way the function will be more generic than a theme based. Anyway, thank you once again.
    – Prajwol
    Aug 7, 2016 at 17:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.