$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>permalinks - Pagination using paginate_links|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

permalinks - Pagination using paginate_links

matteradmin6PV0评论

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/

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far