سؤال

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.

هل كانت مفيدة؟

المحلول

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.

نصائح أخرى

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى wordpress.stackexchange
scroll top