简体   繁体   English

WordPress分页在帖子名称为Permalink的首页中不起作用

[英]Wordpress pagination not working in home page with Post name Permalink

EDIT : Even after making the Permalink Settings to Post name, http://domain.com/?paged=3 still works. 编辑 :即使在将“永久链接设置”设置为“帖子”名称后, http://domain.com/?paged=3仍然有效。

I have posts collection in my home page which has pagination. 我在主页上有分页的帖子集。 Let's say I have 假设我有

domain.com

If my Permalink settings is in default which is Plain Format like 如果我的永久链接设置为默认设置,例如

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

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

How ever if I want my permalink settings to be in Post Name Format like 如果我希望我的永久链接设置采用邮政名称格式,例如

    http://domain.com/sample-post/

Then my pagination in my home page will not work anymore. 然后,我在主页上的分页将不再起作用。 I tried inspect element in the link of pagination if set in post name permalink and it's 如果在帖子名称永久链接中设置了分页的链接,我尝试了检查元素

http://domain.com/page/23/

Then what happened is it will not go to page 23. It will always redirect to my home page 然后发生的事情是它将不会转到第23页。它将始终重定向到我的主页

http://domain.com

I already tried this 我已经尝试过了

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

Which I put this code in my functions.php 我把这段代码放在我的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;
}

And it didn't work. 而且它没有用。

Update I checked the functions.php and i see this 更新我检查了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');

It doesn't have woo commerce plugin but It has that code. 它没有woo Commerce插件,但是有该代码。 Hope it helps you find out whats going on? 希望它可以帮助您了解发生了什么事情?

If you are using WP_Query for retrieving posts then change your code to below 如果您使用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
    ) );

Beside other mentioned scenarios, Make sure you do not have the slug of the template you are using the loop on similar to any of the post types exist in the theme. 除了上述提到的方案之外,请确保您没有正在使用循环的模板的错误,类似于主题中存在的任何帖子类型。 If a page slug is similar to the post type slug, the pagination will not work properly. 如果页面信息与帖子类型信息相似,则分页将无法正常工作。

Is is not possible to have a page name and a custom post type with the same name. page namecustom post type使用相同的名称。 If you do, the page url-slug of the page name and custom post type slug are the same and will cause a not found error. 如果这样做,页面名称的页面url-slug和自定义帖子类型的slug相同,将导致未找到错误。

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

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