permalinks - Pagination using paginate_links

admin2025-06-01  3

I am using the built in paginate_links function and setting formatparameter to the default value:
'?paged=%#%' . For some reason, it keeps redirecting to a different formatted url. For example, when I click the paginated link to page two it should be www.domain/subpage/?paged=2, but instead it's formatted as www.domain/subpage/page/2 . Not sure how to stop this redirect? Any ideas?

Here's the function with the parameters I'm using:

function custom_page_navi( $totalpages, $page, $end_size, $mid_size )
{
    $bignum = 999999999;

    if ( $totalpages <= 1 || $page > $totalpages ) return;

        //NOTE: 
    return paginate_links( array(
        'format'        => '?paged=%#%',
        'current'       => max( 1, $page ),
        'total'         => $totalpages,
        'prev_next'     => false,
        'type'          => 'list',
        'show_all'      => false,
        'end_size'      => $end_size,
        'mid_size'      => $mid_size
    ) );
}

I am using the built in paginate_links function and setting formatparameter to the default value:
'?paged=%#%' . For some reason, it keeps redirecting to a different formatted url. For example, when I click the paginated link to page two it should be www.domain/subpage/?paged=2, but instead it's formatted as www.domain/subpage/page/2 . Not sure how to stop this redirect? Any ideas?

Here's the function with the parameters I'm using:

function custom_page_navi( $totalpages, $page, $end_size, $mid_size )
{
    $bignum = 999999999;

    if ( $totalpages <= 1 || $page > $totalpages ) return;

        //NOTE: https://codex.wordpress/Function_Reference/paginate_links
    return paginate_links( array(
        'format'        => '?paged=%#%',
        'current'       => max( 1, $page ),
        'total'         => $totalpages,
        'prev_next'     => false,
        'type'          => 'list',
        'show_all'      => false,
        'end_size'      => $end_size,
        'mid_size'      => $mid_size
    ) );
}
Share Improve this question asked Jul 24, 2017 at 18:17 bhoodbhood 2572 gold badges3 silver badges10 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

This is a problem with the paginate_links function and having your default permalink structure set to postname/custom but wanting the pagination to use default link structure.

If you look at the code - https://core.trac.wordpress/browser/tags/4.8/src/wp-includes/general-template.php#L0

You can see the following at the top of the paginate_links function

// Setting up default values based on the current URL.
$pagenum_link = html_entity_decode( get_pagenum_link() );
$url_parts    = explode( '?', $pagenum_link );

Then lower you will find the following:

// Merge additional query vars found in the original URL into 'add_args' array.
if ( isset( $url_parts[1] ) ) {
    Bunch of code here to set the format...
}

So the problem is when you use the postname permalink structure the $url_parts variable will only return 1 array value and the custom format checks only run if there are 2 return values in the array. And the code inside this if statement is what parses the format argument.

Therefore your format argument is completely ignored :(

If you go to Settings > Permalinks and you set your structure to "Plain" you should see the pagination links working as you would like.

You could try filtering the 'get_pagenum_link' function to return a "plain" link since the main issue is that this function ignores the format defined in paginate_links and instead uses the $wp_rewrite->using_permalinks() check - https://developer.wordpress/reference/functions/get_pagenum_link/

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748767855a313745.html

最新回复(0)