4

我创建了一个使用内置函数的函数paginate_links

但是分页无法正常工作,因为 URL 写错了。

我想要的 URL 看起来像这样domain.com/properties/page/2/?foo=bar

正在输出的 URL 是domain.com/properties/?foo=bar/page/2/

这是我的代码

function paginate($max_num_pages) {
    global $wp_query, $wp_rewrite;
    $wp_query->query_vars['page'] > 1 ? $current = $wp_query->query_vars['page'] : $current = 1;
    $pagination = array(
        'base' => @add_query_arg('page','%#%'),
        'format'       => '',
        'total'        => $max_num_pages,
        'current'      => $current,
        'show_all'     => true,
        'end_size'     => 1,
        'mid_size'     => 2,
        'prev_next'    => True,
        'prev_text'    => __('« Previous'),
        'next_text'    => __('Next »'),
        'type'         => 'plain',
        'add_args'     => false,
        'add_fragment' => ''
    );

    if( $wp_rewrite->using_permalinks() ) 
        $pagination['base'] = user_trailingslashit( trailingslashit( remove_query_arg( 's', get_pagenum_link( 1 ) ) ) . 'page/%#%/', 'page' );

    if( !empty($wp_query->query_vars['s']) ) 
        $pagination['add_args'] = array( 's' => get_query_var( 's' ) );

    echo paginate_links( $pagination );
}
4

2 回答 2

3

这就是最终为我工作的东西。

我不得不更改$wp_query->query_varstopaged并删除了检查我是否使用永久链接的行if ( $wp_rewrite->using_permalinks() ) $pagination['base'] = user_trailingslashit( trailingslashit( remove_query_arg( 's', get_pagenum_link( 1 ) ) ) . 'page/%#%/', 'paged' );

function paginate($max_num_pages) {
    global $wp_query, $wp_rewrite;
    $wp_query->query_vars['paged'] > 1 ? $current = $wp_query->query_vars['paged'] : $current = 1;
    $pagination = array(
        'base'  => @add_query_arg('paged','%#%'),
        'format'       => '',
        'total'        => $max_num_pages,
        'current'      => $current,
        'show_all'     => true,
        'end_size'     => 1,
        'mid_size'     => 2,
        'prev_next'    => True,
        'prev_text'    => __('« Previous'),
        'next_text'    => __('Next »'),
        'type'         => 'plain',
        'add_args'     => false,
        'add_fragment' => ''
    );

    //if ( $wp_rewrite->using_permalinks() ) $pagination['base'] = user_trailingslashit( trailingslashit( remove_query_arg( 's', get_pagenum_link( 1 ) ) ) . 'page/%#%/', 'paged' );
    if ( !empty($wp_query->query_vars['s']) ) $pagination['add_args'] = array( 's' => get_query_var( 's' ) );  
    echo paginate_links( $pagination );
}
于 2013-08-08T14:10:16.433 回答
1

根据文档,paginate_links默认值format看起来与您所追求的相似,但您通过设置覆盖该值'format' => ''

尝试删除该选项或将其设置为您所追求的:'format' => '?foo=bar%#%'

于 2013-07-31T18:07:31.027 回答