WordPress分页无法在主页上使用帖子名称固定链接

时间:2016-01-20 08:06:17

标签: php wordpress pagination permalinks

编辑:即使将永久链接设置设为帖子名称,http://domain.com/?paged=3仍然有效。

我的主页上有帖子集,有分页。假设我有

domain.com

如果我的永久链接设置是默认的,则为纯文本格式,如

    http://domain.com/?p=123

然后我的分页http://domain.com/?paged=3将起作用。

如果我希望我的永久链接设置像

这样的邮政编码格式
    http://domain.com/sample-post/

然后我的主页中的分页将不再有效。如果在帖子名称永久链接中设置并且它是

,我尝试在分页链接中检查元素
http://domain.com/page/23/

然后发生的事情是它不会转到第23页。它将始终重定向到我的主页

http://domain.com

我已经尝试过这个

https://wordpress.stackexchange.com/questions/134339/pagination-on-custom-post-type-not-working-if-permalinks-set-to-rewrite-url

我将此代码放在我的functions.php

add_filter( 'redirect_canonical','custom_disable_redirect_canonical' ); 
 function custom_disable_redirect_canonical( $redirect_url ){
 if ( is_singular('your_custom_post_type') ) $redirect_url = false;
 return $redirect_url;
}

它不起作用。

更新我检查了functions.php,我看到了这个

/*  -----------------------------------------------------------------------------
        Woo commerce
     */

    if (in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' )))) { // check if we have woo commerce installed
        // breadcrumb
        add_filter( 'woocommerce_breadcrumb_defaults', 'td_woocommerce_breadcrumbs' );

        function td_woocommerce_breadcrumbs() {
            return array(
                'delimiter' => ' <span class="td-sp td-sp-breadcrumb-arrow td-bread-sep"></span> ',
                'wrap_before' => '<div class="entry-crumbs" itemprop="breadcrumb">',
                'wrap_after' => '</div>',
                'before' => '',
                'after' => '',
                'home' => _x( 'Home', 'breadcrumb', 'woocommerce' ),
            );
        }

        // number of products to display on shop page
        add_filter('loop_shop_per_page', create_function('$cols', 'return 8;'));



        if (!function_exists('woocommerce_pagination')) {
            // pagination
            function woocommerce_pagination(){
                echo td_page_generator::get_pagination();
            }
        }


        if (!function_exists('woocommerce_output_related_products')) {
            // number of related product
            function woocommerce_output_related_products() {
                woocommerce_related_products(4,4); // Display 4 products in rows of 4
            }
        }
    }



    /**
     * Add prev and next links to a numbered link list - the pagination on single.
     */
    function wp_link_pages_args_prevnext_add($args)
    {
        global $page, $numpages, $more, $pagenow;

        if (!$args['next_or_number'] == 'next_and_number')
            return $args; # exit early

        $args['next_or_number'] = 'number'; # keep numbering for the main part
        if (!$more)
            return $args; # exit early

        if($page-1) # there is a previous page
            $args['before'] .= _wp_link_page($page-1)
                . $args['link_before']. $args['previouspagelink'] . $args['link_after'] . '</a>'
            ;

        if ($page<$numpages) # there is a next page
            $args['after'] = _wp_link_page($page+1)
                . $args['link_before'] . $args['nextpagelink'] . $args['link_after'] . '</a>'
                . $args['after']
            ;

        return $args;
    }
    add_filter('wp_link_pages_args', 'wp_link_pages_args_prevnext_add');
    add_theme_support('woocommerce');

它没有woo commerce插件,但它有代码。希望它可以帮助您了解最新情况?

3 个答案:

答案 0 :(得分:0)

如果您使用WP_Query检索帖子,请将您的代码更改为

$paged = ( $_REQUEST['paged'] ) ? absint( $_REQUEST['paged'] ) : 1;


    $args = array(
            'posts_per_page' => 5,
            'post_type' => 'post',
            'paged' => $paged,
    );

    $the_query = new WP_Query( $args );

    while ( $the_query->have_posts() ) : $the_query->the_post();
     the_title();
     echo '<br>';
    endwhile;

    $big = 999999999; 

    echo paginate_links( array(
            'format' => '?paged=%#%',
            'current' => max( 1, $paged ),
            'total' => $the_query->max_num_pages
    ) );

答案 1 :(得分:0)

除了上面提到的其他情况外,请确保您没有使用正在循环上的模板的子弹,类似于主题中存在的任何帖子类型。如果页面信息与帖子类型信息相似,则分页将无法正常工作。

答案 2 :(得分:0)

不可能有page namecustom post type同名。如果这样做,页面名称的页面url-slug和自定义帖子类型的slug相同,将导致未找到错误。