WordPress:强制生成第1页的分页URL

时间:2015-04-23 12:45:43

标签: php wordpress pagination

我试图为第一页生成分页网址。我使用了以下代码。

get_pagenum_link(1); // gives http://www.website.com/

总之,

get_pagenum_link(2); // gives http://www.website.com/paged/2/

get_pagenum_link检查页面#if($paged == 1) {// Don't include Page# in URL }我要禁用此检查,以便它始终包含页面#in URL

此外,http://www.website.com/paged/1/被重定向到http://www.website.com/我添加了过滤器

add_filter('redirect_canonical','pif_disable_redirect_canonical');
function pif_disable_redirect_canonical($redirect_url) {
    return false;
}

但是,我认为,这不是一种正确的方法,它会阻止所有规范的重定向。

1 个答案:

答案 0 :(得分:0)

将此功能添加到主题的functions.php

table1 - together_table_id,x,y,z,df,gt,er,re,re    
table2 - together_table_id,q,w,c,v,t,v,b,t,l,f    
table3 - together_table_id,b,n,m,j,h,g,d,f,r,e

用法:

要调用此函数,您必须在主题的index.php,archive.php,category.php或任何其他页面模板中添加以下php代码。

function custom_pagination() {

    if( is_singular() )
        return;

    global $wp_query;

    /** Stop execution if there's only 1 page */
    if( $wp_query->max_num_pages <= 1 )
        return;

    $paged = get_query_var( 'paged' ) ? absint( get_query_var( 'paged' ) ) : 1;
    $max   = intval( $wp_query->max_num_pages );

    /** Add current page to the array */
    if ( $paged >= 1 )
        $links[] = $paged;

    /** Add the pages around the current page to the array */
    if ( $paged >= 3 ) {
        $links[] = $paged - 1;
        $links[] = $paged - 2;
    }

    if ( ( $paged + 2 ) <= $max ) {
        $links[] = $paged + 2;
        $links[] = $paged + 1;
    }

    echo '<div class="navigation"><ul>' . "\n";

    /** Previous Post Link */
    if ( get_previous_posts_link() )
        printf( '<li>%s</li>' . "\n", get_previous_posts_link() );

    /** Link to first page, plus ellipses if necessary */
    if ( ! in_array( 1, $links ) ) {
        $class = 1 == $paged ? ' class="active"' : '';

        printf( '<li%s><a href="%s">%s</a></li>' . "\n", $class, esc_url( get_pagenum_link( 1 ) ), '1' );

        if ( ! in_array( 2, $links ) )
            echo '<li>…</li>';
    }

    /** Link to current page, plus 2 pages in either direction if necessary */
    sort( $links );
    foreach ( (array) $links as $link ) {
        $class = $paged == $link ? ' class="active"' : '';
        printf( '<li%s><a href="%s">%s</a></li>' . "\n", $class, esc_url( get_pagenum_link( $link ) ), $link );
    }

    /** Link to last page, plus ellipses if necessary */
    if ( ! in_array( $max, $links ) ) {
        if ( ! in_array( $max - 1, $links ) )
            echo '<li>…</li>' . "\n";

        $class = $paged == $max ? ' class="active"' : '';
        printf( '<li%s><a href="%s">%s</a></li>' . "\n", $class, esc_url( get_pagenum_link( $max ) ), $max );
    }

    /** Next Post Link */
    if ( get_next_posts_link() )
        printf( '<li>%s</li>' . "\n", get_next_posts_link() );

    echo '</ul></div>' . "\n";

}