paginate_links ignore my format

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.com/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.com/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="https://wordpress.stackexchange.com/questions/275527/?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="https://wordpress.stackexchange.com/questions/275527/?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!

Leave a Comment