简体   繁体   English

分页不适用于 CPT/Taxonomy 的自定义 URL 结构

[英]Pagination not working with custom URL structure of CPT/Taxonomy

I'm fairly new to posting here and I'll try to keep it as clear as possible!我是在这里发帖的新手,我会尽量保持清晰! Any guidance is greatly appreciated!非常感谢任何指导!

The Goal:目标:


I am trying to create a Custom Post Type and a Taxonomy where I can upload posts inside the Taxonomy and have a URL structure like this: site-name.com/cpt-slug/taxonomy-slug/post-slu .我正在尝试创建一个自定义帖子类型和一个分类法,我可以在其中上传分类法中的帖子并具有如下 URL 结构: site-name.com/cpt-slug/taxonomy-slug/post-slu

I also need to be able to have pagination on both site-name.com/cpt-slug/ and site-name.com/cpt-slug/taxonomy-slug我还需要能够在site-name.com/cpt-slug/site-name.com/cpt-slug/taxonomy-slug上进行分页

Currently:目前:


I have studied a few other posts that have gotten me 99% of the way there!我研究了其他一些帖子,这些帖子已经让我达到了 99%! I just can't quite figure out how to finish it at this point.我只是无法弄清楚如何在这一点上完成它。 I have done most of what this answer has suggested and i will show my code below for clarity.我已经完成了这个答案所建议的大部分内容,为了清楚起见,我将在下面显示我的代码。

CPT registration CPT注册

register_post_type( 'knowledge_base',
        array(
            'labels' => array(
                'name' => __( 'Knowledge Base' ),
                'singular_name' => __( 'Knowledge Base Post' )
            ),
            'public' => true,
            'has_archive' => true,
            'menu_position' => 25,
            'menu_icon' => 'dashicons-book',
            'hierarchical' => true,
            'supports' => array(
                'title',
                'editor',
                'excerpt',
                'page-attributes',
                'thumbnail'
            ),
            'taxonomies' => array('kb_topics'),
            'rewrite' => array(
                'slug' => 'kb/%taxonomy_name%',
                'with_front' => false
            ),
        )
    );

Custom taxonomy registration自定义分类注册

register_taxonomy(
        'kb_topics',
        'knowledge_base',
        array(
            'label' => 'Categories',
            'hierarchical' => true,
            'rewrite' => array(
                'slug' => 'kb',
                'with_front' => false
            ),
        )
    );

Tell WordPress how to interpret my knowledge base URL structure:告诉 WordPress 如何解释我的知识库 URL 结构:

function add_rewrite_rules( $rules ) {
  $new = array();
  $new['kb/([^/]+)/(.+)/?$'] = 'index.php?knowledge_base=$matches[2]';
  $new['kb/(.+)/?$'] = 'index.php?kb_topics=$matches[1]';

  return array_merge( $new, $rules ); // Ensure our rules come first
}
add_filter( 'rewrite_rules_array', 'add_rewrite_rules' );

Handle the %taxonomy_name% URL placeholder处理%taxonomy_name% URL 占位符

function filter_post_type_link( $link, $post ) {
  if ( $post->post_type == 'knowledge_base' ) {
    if ( $cats = get_the_terms( $post->ID, 'kb_topics' ) ) {
      $link = str_replace( '%taxonomy_name%', current( $cats )->slug, $link );
    }
  }
  return $link;
}
add_filter( 'post_type_link', 'filter_post_type_link', 10, 2 );

The problem:问题:


Overall the post I referenced makes a ton of sense, I feel I understand whats going on here and everything was good until I tried setting up Pagination on the page site-name.com/kb .总的来说,我引用的帖子很有意义,我觉得我明白这里发生了什么,一切都很好,直到我尝试在页面site-name.com/kb上设置分页。

This page was working great.此页面运行良好。 I was able to show all the posts per category and I was able to go from here and click any post or Taxonomy and have the URL structure mentioned above.我能够显示每个类别的所有帖子,我能够从这里点击任何帖子或分类法,并具有上述 URL 结构。 However whenever I try to go to the next page or third page I either get a 404 error or I get redirected to a post in my CPT.然而,每当我尝试转到下一页或第三页时,我要么收到 404 错误,要么被重定向到我的 CPT 中的帖子。

As an example site-name.com/kb/page/2 always goes to the same post that is a post in my CPT and site-name.com/kb/page/3 always goes to 404. After some digging around some more I found another post that seemed to have a very promising answer and I still feel it could be the right answer I just can't get it to work.例如, site-name.com/kb/page/2总是转到与我的 CPT 中的帖子相同的帖子,而site-name.com/kb/page/3总是转到 404。经过一些挖掘之后我发现另一个帖子似乎有一个非常有希望的答案,但我仍然觉得它可能是正确的答案,但我无法让它发挥作用。 Admittedly I don't have a lot of experience with rewrites and this may be where the issue is.不可否认,我在重写方面没有很多经验,这可能是问题所在。

Here is my version of this user's suggestion:这是我对该用户建议的版本:

function fix_kb_category_pagination( $wp_rewrite ) {
    unset($wp_rewrite->rules['kb/([^/]+)/page/?([0-9]{1,})/?$']);
    $wp_rewrite->rules = array(
        'kb/?$' => $wp_rewrite->index . '?post_type=knowledge_base',
        'kb/page/?([0-9]{1,})/?$' => $wp_rewrite->index . '?post_type=knowledge_base&paged=' . $wp_rewrite->preg_index( 1 ),
        'kb/([^/]+)/page/?([0-9]{1,})/?$' => $wp_rewrite->index . '?kb_topics=' . $wp_rewrite->preg_index( 1 ) . '&paged=' . $wp_rewrite->preg_index( 2 ),
    ) + $wp_rewrite->rules;
}
add_action( 'generate_rewrite_rules', 'fix_kb_category_pagination' );

I have tested the pagination and my page template by creating a different page with a slug of /kb-test/ and everything works perfectly.我已经通过使用/kb-test/ slug 创建一个不同的页面来测试分页和我的页面模板,并且一切正常。 So this only happens on /kb/ for some reason.因此,出于某种原因,这只发生在/kb/

From what I understand from the posts I mentioned above, WordPress has created rewrites based on the CPT and Taxonomy I set up and so its not able to go to /kb/page/ , but I have tried many times now tweaking the code I have here trying to get it to recognize /kb/page/ but to no avail.从我从上面提到的帖子中了解到,WordPress 已经根据我设置的 CPT 和分类法创建了重写,因此它无法转到/kb/page/ ,但我已经尝试了很多次,现在正在调整我的代码这里试图让它识别/kb/page/但无济于事。

Thank you ahead of time to anyone who takes the time to look through this and respond.提前感谢所有花时间查看并回复的人。 I really think I must be super close but just cant quite get the last bit alone.我真的认为我必须非常接近,但不能完全独自完成最后一点。 Thanks everyone!谢谢大家!

** **

UPDATE更新

** **

First off thank you for everyone helping me format my question, much appreciated!首先感谢大家帮助我格式化我的问题,非常感谢! I wanted to write a quick update to help answer this question for future viewers if I can.如果可以的话,我想写一个快速更新来帮助将来的观众回答这个问题。

After working on this more I now realize I was very close with the code above.在进一步研究之后,我现在意识到我非常接近上面的代码。 One thing I had to do was remove the rewrite_rules_array hook.我必须做的一件事是删除rewrite_rules_array钩子。 I assume because the rules were conflicting with the rules I have in the generate_rewrite_rules hook.我假设是因为规则与我在generate_rewrite_rules钩子中的规则相冲突。

So that's great!太好了! However I still have one remaining issue i'm working on.但是我还有一个问题正在解决。 For some reason when I go to site-name.com/kb/page/2/ it still goes to a post that is in the /kb/ CPT.出于某种原因,当我访问site-name.com/kb/page/2/它仍然会转到/kb/ CPT 中的帖子。 Every other page seems to work great.其他所有页面似乎都运行良好。 site-name.com/kb/page/3/ and so on, all work correctly. site-name.com/kb/page/3/等,一切正常。 I even deleted the post that /page/2/ is going to.我什至删除了/page/2/将要发布的帖子。 It still goes to that same URL but now just shows 404.它仍然转到同一个 URL,但现在只显示 404。

I'll keep working on this and update when I figure it out.我会继续努力,并在我弄清楚时更新。 In the mean time if anyone has any tips to help with this i'd appreciate any help!同时,如果有人有任何提示可以帮助解决此问题,我将不胜感激!

** **

FINAL UPDATE最后更新

** **

Turns out the last little issue of /page/2/ was a caching issue and all is good now.原来/page/2/的最后一个小问题是缓存问题,现在一切都很好。 I will answer my question so we can close it out.我会回答我的问题,以便我们可以关闭它。 Hopefully this helps anyone in the future with this same issue!希望这可以帮助将来遇到同样问题的任何人!

To anyone running into issues with this I hope my experiences help!对于遇到此问题的任何人,我希望我的经验有所帮助! Here are the steps I believe you need to follow to get it working correctly:以下是我认为您需要遵循的步骤才能使其正常工作:

Custom Post Type Registration - Include rewrite structure like this (kb being whatever slug you want in URL for CPT)自定义帖子类型注册 - 包括这样的重写结构(kb 是您在 CPT 的 URL 中想要的任何 slug)

'rewrite' => array(
                'slug' => 'kb/%taxonomy_name%',
                'with_front' => false
            ),

Custom Taxonomy Registration - Include rewrite structure (same as slug above)自定义分类注册 - 包括重写结构(与上面的 slug 相同)

'rewrite' => array(
                'slug' => 'kb',
                'with_front' => false
            ),

Include this hook to tell Wordpress how to handle %taxonomy_name% in your CPT registration (for me "knowledge_base" is my CPT name, and "kb_topics" is my taxonomy name. Replace these as necessary throughout)包含这个钩子来告诉 Wordpress 如何在你的 CPT 注册中处理 %taxonomy_name%(对我来说,“knowledge_base”是我的 CPT 名称,而“kb_topics”是我的分类名称。根据需要在整个过程中替换这些)

function filter_post_type_link( $link, $post ) {
  if ( $post->post_type == 'knowledge_base' ) {
    if ( $cats = get_the_terms( $post->ID, 'kb_topics' ) ) {
      $link = str_replace( '%taxonomy_name%', current( $cats )->slug, $link );
    }
  }
  return $link;
}
add_filter( 'post_type_link', 'filter_post_type_link', 10, 2 );

Lastly you need to include some rewrites to fix the pagination最后,您需要进行一些重写以修复分页

function fix_kb_category_pagination( $wp_rewrite ) {
    unset($wp_rewrite->rules['kb/([^/]+)/page/?([0-9]{1,})/?$']);
    $wp_rewrite->rules = array(
        'kb/?$' => $wp_rewrite->index . '?post_type=knowledge_base',
        'kb/page/?([0-9]{1,})/?$' => $wp_rewrite->index . '?post_type=knowledge_base&paged=' . $wp_rewrite->preg_index( 1 ),
        'kb/([^/]+)/page/?([0-9]{1,})/?$' => $wp_rewrite->index . '?kb_topics=' . $wp_rewrite->preg_index( 1 ) . '&paged=' . $wp_rewrite->preg_index( 2 ),
    ) + $wp_rewrite->rules;
}
add_action( 'generate_rewrite_rules', 'fix_kb_category_pagination' );

TIP: One last tip, in order to use a page template for site-name.com/kb/ you need to create a template in the back end with the filename of archive-knowledge_base.php , replacing knowledge_base with your CPT name.提示:最后一个提示,为了使用site-name.com/kb/的页面模板,您需要在后端创建一个模板,文件名为archive-knowledge_base.php ,将knowledge_base替换为您的CPT 名称。

That's it!就是这样! I hope this helps others in the future maybe not run into the same issues I did.我希望这可以帮助其他人将来可能不会遇到与我相同的问题。 Good luck and happy coding!祝你好运,编码愉快!

Use this code below:使用下面的代码:

function column_custom_rewrite_basic( $wp_rewrite ) {
    unset($wp_rewrite->rules['column/([^/]+)/page/?([0-9]{1,})/?$']);
    $feed_rules = array(
        'column/page/?([0-9]{1,})/?$' => $wp_rewrite->index . '?post_type=column&paged=' . $wp_rewrite->preg_index( 1 ),
        'column/([^/]+)/page/?([0-9]{1,})/?$' => $wp_rewrite->index . '?tax_column_category=' . $wp_rewrite->preg_index( 1 ) . '&paged=' . $wp_rewrite->preg_index( 2 ),
        'column/(.+)/(.+)/?$' => $wp_rewrite->index . '?post_type=column&tax_column_category=' . $wp_rewrite->preg_index( 1 ) . '&name=' . $wp_rewrite->preg_index( 2 ),
        'column/(.+)?/?$' => $wp_rewrite->index . '?post_type=column&tax_column_category=' . $wp_rewrite->preg_index( 1 ),
    );

    $wp_rewrite->rules = $feed_rules + $wp_rewrite->rules;
    return $wp_rewrite->rules;
}

add_filter('generate_rewrite_rules', 'column_custom_rewrite_basic');

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

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