Cannot go back to the first paginated page using pagination links

I embedded standard code for pagination links and everything is working fine except that clicking the Previous or Page 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.

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745333371a4622989.html

相关推荐

  • Cannot go back to the first paginated page using pagination links

    I embedded standard code for pagination links and everything is working fine except that clicking the Previous or Page 1

    5小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信