1

I embedded standard code for pagination links and everything is working fine except that clicking the Previous or Page 1 link does not take me back to the first paginated page. This is different from other problems with pagination links reported elsewhere.

The code for the pagination links is as follows:

    $total_pages = $wp_query->max_num_pages;
    $current_page = max(1, get_query_var('paged'));
    echo paginate_links(array(
        'base'               => '%_%',
        'format'             => '?paged=%#%',
        'show_all'           => false,
        'current' => $current_page,
        'total' => $total_pages,
        'end_size'           => 2,
        'mid_size'           => 2,
        'prev_next'          => true,
        'prev_text'          => __('« Previous'),
        'next_text'          => __('Next »'),
        'type'               => 'plain',
        'add_args'           => false,
        'add_fragment'       => '',
        'before_page_number' => '',
        'after_page_number'  => ''
    ));

The max number of the posts to be displayed is set to 2 and every page is showing two posts.

If you spot any potential problem with the code, please let me know.

Thank you for reading.

2 Answers 2

2

To get the canonical (not ?page=1) reference URL to start the pagination from you have to change

'base' => '%_%'

to

'base' => get_pagenum_link() . '%_%' // get_pagenum_link() default is '1'

which will give you the first page of paginated posts and %_% will be replaced by 'format' parameter on the next pages.

1
  • Thank you, Max Yudin! Replacing the base parameter value indeed fixed the link bug. I understand the function of get_pagenum_link(), but still don't get why it was only the Previous and Page 1 links which went out of order...
    – Fizzler
    Commented Jul 10, 2019 at 9:48
0

It looks like $current_page can be a minimum of 1. in the WP Codex the default is 0. If you change $current_page = max(1, get_query_var('paged')); to $current_page = max(0, get_query_var('paged')); you may get it to work how you want it to work. If this is the solution (or part of it), you may also have to adjust your logic elsewhere to make 0 the first page instead of 1.

1
  • Indeed the Codex states 0 as the default, but in my case changing 1 to 0 in the argument of max() function resulted in the same. I actually copied $current_page = max(1, get_query_var('paged')); from some other forum and think that the author may not have noticed the error or used different parameters for paginate_links()... Anyway, when I delete all of the parameters in the paginate_links argument, the pagination starts working... I'm still unsure what parameter is to be corrected.
    – Fizzler
    Commented Jul 10, 2019 at 3:32

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.