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

wp query - paginate_links ignore my format

programmeradmin1浏览0评论

I need the pagination liks to have the url, like:

current_url?p-page=1
current_url?p-page=2
current_url?p-page=3

And so.. The reason I need this is because I have other params in the page.

the problem is that in the docs, it specs:

format

(string) (optional) Used for Pagination structure. The default value is '?page=%#%', If using pretty permalinks this would be '/page/%#%', where the '%#%' is replaced by the page number. Default: '?page=%#%'

And I have friendly url enabled,

How can I prevent the default ( BTW it ignores my p-page, still using page )

current_url/page/1
current_url/page/2
current_url/page/3

this is my code, right now:

echo paginate_links( array(
    'base'         => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ),
    'total'        => $query->max_num_pages,
    'current'      => max( 1, get_query_var( 'p-page' ) ),
    'format'       => '?p-page=%#%',
    'show_all'     => false,
    'type'         => 'plain',
    'end_size'     => 2,
    'mid_size'     => 1,
    'prev_next'    => true,
    'prev_text'    => sprintf( '<i></i> %1$s', '<i class="icon-chevron-left"></i>' ),
    'next_text'    => sprintf( '%1$s <i></i>', '<i class="icon-chevron-right"></i>' ),
    'add_args'     => false,
    'add_fragment' => '',
) );

As a temporary solution I'm doing it like this:

if ($current_page > 1) {
    echo '<a href="?p-page='.($current_page-1).'" class="page-numbers prev"><i class="icon-chevron-left"></i></a>';
}
for ($i = 1; $i <= $query->max_num_pages; $i++) {
    echo '<a href="?p-page='.$i.'" class="page-numbers '.($current_page == $i ? 'current' : '').'">'.$i.'</a>';
}
if ($current_page < $query->max_num_pages) {
    echo '<a href="?p-page='.($current_page+1).'" class="page-numbers prev"><i class="icon-chevron-right"></i></a>';
}

But would be great to take advantage of the dots functionality and so.. any idea?

I need the pagination liks to have the url, like:

current_url?p-page=1
current_url?p-page=2
current_url?p-page=3

And so.. The reason I need this is because I have other params in the page.

the problem is that in the docs, it specs:

format

(string) (optional) Used for Pagination structure. The default value is '?page=%#%', If using pretty permalinks this would be '/page/%#%', where the '%#%' is replaced by the page number. Default: '?page=%#%'

And I have friendly url enabled,

How can I prevent the default ( BTW it ignores my p-page, still using page )

current_url/page/1
current_url/page/2
current_url/page/3

this is my code, right now:

echo paginate_links( array(
    'base'         => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ),
    'total'        => $query->max_num_pages,
    'current'      => max( 1, get_query_var( 'p-page' ) ),
    'format'       => '?p-page=%#%',
    'show_all'     => false,
    'type'         => 'plain',
    'end_size'     => 2,
    'mid_size'     => 1,
    'prev_next'    => true,
    'prev_text'    => sprintf( '<i></i> %1$s', '<i class="icon-chevron-left"></i>' ),
    'next_text'    => sprintf( '%1$s <i></i>', '<i class="icon-chevron-right"></i>' ),
    'add_args'     => false,
    'add_fragment' => '',
) );

As a temporary solution I'm doing it like this:

if ($current_page > 1) {
    echo '<a href="?p-page='.($current_page-1).'" class="page-numbers prev"><i class="icon-chevron-left"></i></a>';
}
for ($i = 1; $i <= $query->max_num_pages; $i++) {
    echo '<a href="?p-page='.$i.'" class="page-numbers '.($current_page == $i ? 'current' : '').'">'.$i.'</a>';
}
if ($current_page < $query->max_num_pages) {
    echo '<a href="?p-page='.($current_page+1).'" class="page-numbers prev"><i class="icon-chevron-right"></i></a>';
}

But would be great to take advantage of the dots functionality and so.. any idea?

Share Improve this question edited Aug 3, 2017 at 12:58 Toni Michel Caubet asked Aug 2, 2017 at 9:04 Toni Michel CaubetToni Michel Caubet 6163 gold badges11 silver badges33 bronze badges 0
Add a comment  | 

1 Answer 1

Reset to default 11 +50

This part:

'base' => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ),

is generating a page part like this one:

'base' => http://example.tld/page/%#%/

If we peek into paginate_links() we see the default:

'base' => $pagenum_link, // http://example/all_posts.php%_% : 
                         // %_% is replaced by format (below)
'format' => $format,     // ?page=%#% : %#% is replaced by the page number

where the inline comment say that %_% is replaced by the format.

The documentation also says:

An example of the ‘base’ argument is "http://example/all_posts.php%_%" and the ‘%_%’ is required. The ‘%_%’ will be replaced by the contents of in the ‘format’ argument. An example for the ‘format’ argument is "?page=%#%" and the ‘%#%’ is also required. The ‘%#%’ will be replaced with the page number.

If we use that:

'base' => '%_%'

then it will become the same as the format argument.

Here's a modification of OP's example:

echo paginate_links( 
    [
        'base'         => '%_%',
        'total'        => $query->max_num_pages,
        'current'      => $current,
        'format'       => '?p-page=%#%',
        'show_all'     => false,
        'type'         => 'plain',
        'end_size'     => 2,
        'mid_size'     => 1,
        'prev_next'    => true,
        'prev_text'    => '<i></i> <i class="icon-chevron-left"></i>',
        'next_text'    => '<i class="icon-chevron-right"></i> <i></i>',
        'add_args'     => false,
        'add_fragment' => '',
    ]
);

where we use:

$current   = max( 1, (int) filter_input( INPUT_GET, 'p-page' ) );

that will also go into the WP_Query argument of $query:

'paged' => $current,

Example output for ?p-page=6 :

<a class="prev page-numbers" href="?p-page=5"><i></i> <i class="icon-chevron-left"></i></a>
<a class='page-numbers' href=''>1</a>
<a class='page-numbers' href='?p-page=2'>2</a>
<span class="page-numbers dots">&hellip;</span>
<a class='page-numbers' href='?p-page=5'>5</a>
<span class='page-numbers current'>6</span>
<a class='page-numbers' href='?p-page=7'>7</a>
<span class="page-numbers dots">&hellip;</span>
<a class='page-numbers' href='?p-page=99'>99</a>
<a class='page-numbers' href='?p-page=100'>100</a>
<a class="next page-numbers" href="?p-page=7"><i class="icon-chevron-right"></i> <i></i></a>

ps: There's no need for sprintf to combine two static strings, as we see in the OP for prev_text and next_text. Currently that part looks wrong in the original snippet, so I removed it.


Hope it helps!

发布评论

评论列表(0)

  1. 暂无评论