paginate_links ignore my format

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?

Topic query-variable wp-query urls pagination Wordpress

Category Web


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="?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!

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.