简体   繁体   English

在wordpress网站上的分页正在使用普通的永久链接(com /?p = 123),但无法使用自定义(/%postname%/)

[英]Pagination at wordpress site is working with plain permalinks (com/?p=123) but not with custom (/%postname%/)

I am trying to create my first website in wordpress and i am facing problem with the pagination. 我正在尝试在wordpress中创建我的第一个网站,但我面临分页问题。 I spent two days to figure out what the problem is but i cant... The pagination is working when i set the permalinks to plain and the site's redirect (which is like this /?page_id=6032&paged=2) is fine. 我花了两天的时间弄清楚问题出在哪里,但我不能...当我将永久链接设置为纯文本并且站点的重定向(例如/?page_id = 6032&paged = 2)时,分页正常。 But when i set the permalinks to custom (the redirect is sending me to /accommodation/page/2/) gets me to a page not found. 但是,当我将永久链接设置为自定义(重定向将我发送到/ accommodation / page / 2 /)时,将我引导至未找到的页面。 I 've searched a lot this days and found many solutions but nothing seems to work. 这几天我进行了很多搜索,发现了很多解决方案,但似乎没有任何效果。 The odd thing is that i used the exact same pagination to another page of my site and the it works properly. 奇怪的是,我对网站的另一页使用了完全相同的分页,因此它可以正常工作。 The accommodation page where pagination is not working is like this 分页不起作用的住宿页面是这样的

 <?php
        $accommodation_perpage = '6';           
        $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;     
        query_posts( "post_type=accommodation&posts_per_page=$accommodation_perpage&paged=$paged" );                
        if( have_posts() ) :
            while( have_posts() ) : the_post(); ?>

and the testimonials page which is working is the same with only difference 和正在工作的推荐页面相同,只是有所不同

query_posts( "post_type=testimonial&posts_per_page=$testimonial_perpage&paged=$paged" )

My pagination's code is 我的分页代码是

 function 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";
}

please add this code in your function.php file 请将此代码添加到您的function.php文件中

function custom_numeric_pagination() {
        if (is_singular())
            return;
        global $wp_query;
        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);
        if ($paged >= 1)
            $links[] = $paged;
        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";
        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>';
        }
        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);
        }
        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);
        }
        echo '</ul></div>' . "\n";
    }


function na_parse_request( $query ) {

    if ( ! $query->is_main_query() || 2 != count( $query->query ) || ! isset( $query->query['page'] ) ) {
        return;
    }

    if ( ! empty( $query->query['name'] ) ) {
        $query->set( 'post_type', array( 'post', 'yourcustomposttypename', 'page' ) );
    }
}
add_action( 'pre_get_posts', 'na_parse_request' );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM