自定义帖子类型和自定义重写规则的Wordpress分页问题 [英] Wordpress pagination issues with Custom Post Type and custom rewrite rule

查看:91
本文介绍了自定义帖子类型和自定义重写规则的Wordpress分页问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们使用最新的WP(5.2.2).在安装过程中,我们有一个名为Stories的自定义帖子类型(代码:stories),具有以下功能.

We use latest WP (5.2.2). We have a custom post type called Stories (slug: stories) with the following capabilities during setup.

    Public: true
    Publicly Queryable: true
    Show UI: true
    Has Archive: true
    Exclude From Search: false
    Hierarchical: false
    Rewrite: true
    Custom Rewrite Slug: stories/%category%
    Show in Nav Menus: true
    Show in REST API: true
    With Front: true
    Query Var: true

这很好.例如,我们创建了一个名为"Huckleberry Cafe Opens"的新故事,并将其分配给一个名为"Food"的类别,URL可以正常工作:

This works fine. For example, we create a new story called "Huckleberry Cafe Opens" and assign it to a category called "Food", and the URL works:

site.com/stories/food/huckleberry-cafe-open

我们的永久链接设置为/%category%/%postname%,但是对于自定义帖子类型,我们还使用有用的插件 Permalink Manager Lite .这包含永久结构",我们使用与上面自定义帖子类型中的自定义重写段符"匹配的模式:

Our permalinks are set to /%category%/%postname%, but for custom post types we also use the helpful plugin, Permalink Manager Lite. This contains "permastructures" and we use a matching pattern to the "custom rewrite slug" from our custom post type above:

stories/%category%/%stories%

我们自然在archive-stories.php中有一个类别页面.这样,无论类别是食品,时尚还是家庭维修,相同的整体主题都适用于此自定义帖子类型.

We naturally have a category page for this, in archive-stories.php. This way whether the category was Food, or Fashion, or Home Repairs, the same overall theme works for this custom post type.

问题在于分页.有了本网站的一些有用评论以及类似这些帖子,我可以现在终于看到了页码.

The problem lies with Pagination. With some helpful commentary from this website and posts like these, I can now finally see the page numbers.

问题: 单击Page 2或其他页面只会给我与Page 1一样的东西.

PROBLEM: Clicking on Page 2 or beyond just gives me the same thing as Page 1.

我使用了很有帮助的来自Github的永久链接调试脚本,它确实向我展示了使用的模板"在第1页和第2页上有所不同.

I used the helpful permalink debug script from Github and it does show me that the "template used" varies across page 1 and 2.

第1页:/故事/食物 使用通常的category.php(为什么?这是自定义帖子类型!)

Page 1: /stories/food Uses the usual category.php (Why? This is a custom post type!)

第2页:/stories/food/page/2 使用更可预测的archive-stories.php

请注意,在我们网站的所有自定义帖子类型中,这些类别都是相同的.我们有自定义帖子类型,例如故事",商店"等.它们都被分配了相同的基本类别(食物",样式",上门维修" ..).

Note that these categories are the same across all our website's custom post types. We have custom post types like Stories, Shop, etc. All of them are assigned the same base categories (Food, Style, Home Repairs..).

在archive-stories.php和category.php(目前,为了进行测试,它们完全相同的文件)中,我们在顶部具有以下WP_Query:

In both archive-stories.php and category.php (which are currently, for testing, the exact same file), we have the following WP_Query up top:

<?php

$paged = get_query_var('paged') ? get_query_var('paged') : 1;
$args = array(
        'post_type'      => 'stories',
        'orderby'        => 'date',
        'order'          => 'DESC',
        'posts_per_page' => 10,
        'post_status'    => array('publish', 'pending', 'draft'),
        'paged'          => $paged
    );

$the_query = new WP_Query($args);
while( $the_query->have_posts() ) : $the_query->the_post(); 

 ?>

 ......LOOP HERE.....

<?php


// DO THE "TOTAL PAGES" THING
$total_pages = $the_query->max_num_pages;
if ($total_pages > 1){

    $current_page = max(1, get_query_var('paged'));
    echo paginate_links(array(
        'base' => get_pagenum_link(1) . '%_%',
        'format' => '/page/%#%',
        'current' => $current_page,
        'total' => $total_pages,
        'prev_text'    => __('« prev'),
        'next_text'    => __('next »'),
    ));
}    


?>

这确实很好地显示了循环本身,并且正确显示了分页.但是,单击第2页及其后的页面始终会显示与第1页完全相同的页面.

This does show the loop itself well, and it shows the pagination correctly. But clicking page 2 and beyond always shows the exact same page as page 1.

顺便说一句,如果我们用print_r($the_query);转储WP_Query(),我们将看到正在创建的SQL对于页面1和页面2完全相同,这说明了为什么页面相同. LIMIT子句中的偏移"值始终为0.在第2页上,该值应为10,在第3页上,该值应为20,依此类推.但是我们不确定为什么它为0.

Btw, if we dump the WP_Query() with a print_r($the_query);, we see that the SQL being created is the exact same for both page 1 and page 2, which explains why the page is the same. The "offset" value in LIMIT clause is always 0. On Page 2, this should be 10, on Page 3, this should be 20, etc. We're not sure why it is 0 though.

SELECT SQL_CALC_FOUND_ROWS  
    wp_posts.ID 
FROM wp_posts  
WHERE 1=1  
      AND wp_posts.post_type = 'stories' 
      AND ((wp_posts.post_status = 'publish' OR wp_posts.post_status = 'draft' OR wp_posts.post_status = 'pending'))  
      ORDER BY wp_posts.post_date 
      DESC LIMIT 0, 10
;

如果有帮助,我们注意到我们需要functions.php中的以下代码才能完全运行/page/2,否则它将抛出404.这来自

If it helps, we notice that we need the following code in functions.php for our /page/2 to work at all, otherwise it throws a 404. This is from the plugin for Category Pagination Fix:

     function remove_page_from_query_string($query_string)
    { 
        if (isset($query_string['name']) && $query_string['name'] == 'page' && isset($query_string['page'])) {
            unset($query_string['name']);
            // 'page' in the query_string looks like '/2', so i'm spliting it out
            @list($delim, $page_index) = explode('/', $query_string['page']);
            $query_string['paged'] = $page_index;
        }      
        return $query_string;
    }
    // I will kill you if you remove this. I died two days for this line 
    add_filter('request', 'remove_page_from_query_string');

    // following are code adapted from Custom Post Type Category Pagination Fix by jdantzer
    function fix_category_pagination($qs){
        if(isset($qs['category_name']) && isset($qs['paged'])){
            $qs['post_type'] = get_post_types($args = array(
                'public'   => true,
                '_builtin' => false
            ));
            array_push($qs['post_type'],'post');
        }
        return $qs;
    }
    add_filter('request', 'fix_category_pagination');

非常感谢您的指导!

推荐答案

我不确定它是否可以解决您的问题,但是您可以尝试在$ args中设置偏移量

I´m not sure if its going to solve your problem but you could try to set offset within your $args

$paged = get_query_var('paged') ? get_query_var('paged') : 1;
$posts_per_page = 10;
$offset = ( $paged - 1 ) * $posts_per_page;

$args = array(
        'post_type'      => 'stories',
        'orderby'        => 'date',
        'order'          => 'DESC',
        'posts_per_page' => $posts_per_page,
        'post_status'    => array('publish', 'pending', 'draft'),
        'paged'          => $paged,
        'offset' => $offset
    );

更改后,请尝试清除您的永久链接. Wordpress->设置->永久链接->保存

After the changes try to flush your permalinks. Wordpress->Settings->Permalinks->Save

并清除缓存

这篇关于自定义帖子类型和自定义重写规则的Wordpress分页问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆