最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Cannot go back to the first paginated page using pagination links

programmeradmin2浏览0评论

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.

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.

Share Improve this question asked Jul 10, 2019 at 0:55 FizzlerFizzler 757 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 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.

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.

发布评论

评论列表(0)

  1. 暂无评论