在分页中,将页面1的链接更改为主页

时间:2020-07-30 作者:Luis

我有一个问题,当我在第2页或任何其他页面上,并且我想返回到索引时,在第1页为我提供分页的url如下(https://www.url.com/page/1) 我希望它像往常一样(https://www.url.com)

El código que tengo en El functions公司。php es el siguiente:

function mt_paginacion($tipo=1,$class=NULL,$qc=NULL){

global $wp_query,$paged;

if($qc==\'\'){$total=$wp_query->max_num_pages;}else{$total=$qc;}

if($tipo==1){

$paginacion=paginate_links( array(

    \'base\' => str_replace(999999999, \'%#%\', esc_url( get_pagenum_link(999999999) ) ),

    \'format\' => \'?paged=%#%\',

    \'current\' => max( 1, $paged ),

    \'total\' => $total,

    \'mid_size\' => 3,

    \'prev_next\' => true

) );

1 个回复
SO网友:Dave Romsey

你可以做一个简单的str_replace( \'/page/1/\', \'/\', $string ) 关于产生的结果paginate_links() 摆脱/page/1/ 它显示在第一页链接和上一页链接(当它指向第一页时)上。

下面是一个完整的(经过测试的)示例:

/**
 * Numeric pagination via WP core function paginate_links().
 * @link http://codex.wordpress.org/Function_Reference/paginate_links
 *
 * @param array $srgs
 *
 * @return string HTML for numneric pagination
 */
function wpse_pagination( $args = array() ) {
    global $wp_query;

    if ( $wp_query->max_num_pages <= 1 ) {
        return;
    }

    $pagination_args = array(
        \'base\'         => str_replace( 999999999, \'%#%\', esc_url( get_pagenum_link( 999999999 ) ) ),
        \'total\'        => $wp_query->max_num_pages,
        \'current\'      => max( 1, get_query_var( \'paged\' ) ),
        \'format\'       => \'?paged=%#%\',
        \'show_all\'     => false,
        \'type\'         => \'array\',
        \'end_size\'     => 2,
        \'mid_size\'     => 3,
        \'prev_next\'    => true,
        \'prev_text\'    => __( \'&laquo; Prev\', \'wpse\' ),
        \'next_text\'    => __( \'Next &raquo;\', \'wpse\' ),
        \'add_args\'     => false,
        \'add_fragment\' => \'\',

        // Custom arguments not part of WP core:
        \'show_page_position\' => false, // Optionally allows the "Page X of XX" HTML to be displayed.
    );

    $pagination = paginate_links( array_merge( $pagination_args, $args ) );

    // Remove /page/1/ from links.
    $pagination = array_map(
        function( $string ) {
            return str_replace( \'/page/1/\', \'/\', $string );
        },
        $pagination
    );

    return implode( \'\', $pagination );
}

Usage:

echo wpse_pagination();

相关推荐

Custom pagination structure

当前我的分页工作方式如下http://example.com/city/usa/ #for page 1 http://example.com/city/usa/page/2 http://example.com/city/usa/page/3 ...and so on 我想将其更改为:http://example.com/city/usa/ #for page 1 http:/