简体   繁体   English

WordPress:用于自定义帖子类型类别存档的永久链接重写

[英]WordPress: permalink rewrite for custom post type category archive

I have a custom post type resource . 我有一个自定义帖子类型resource This custom post type shares the category taxonomy with posts. 此自定义帖子类型与帖子共享category分类法。

For a given category term archive, example url: /category/reducing-inequality/ , I want to have an additional URL that displays ONLY the resource custom post type for that category. 对于给定的类别术语存档,例如url: /category/reducing-inequality/ ,我希望有一个附加URL,该URL仅显示该类别的资源自定义帖子类型。 I can do so by using a query string: 我可以通过使用查询字符串来做到这一点:

category/reducing-inequality/?post_type=resource

What I want to do though is create a rewrite rule that will have this category archive (the above URL) instead rewrite to /resource/category/reducing-inequality 我要做的是创建一个重写规则,该规则将具有该类别存档(上述URL),而不是重写为/resource/category/reducing-inequality

Is this possible? 这可能吗? Any guidance is much appreciated, thank you! 任何指导,不胜感激,谢谢!

This is exactly what add_rewrite_rule() is for. 这正是add_rewrite_rule()的用途。 I'd do something like: 我会做类似的事情:

function custom_resource_rewrite_rules() {
    add_rewrite_rule('^(\w+)/category/([A-Za-z0-9\-\_]+)/?', 'category/$matches[2]/?post_type=$matches[1]', 'top');
}
add_action('init', 'custom_resource_rewrite_rules');

You could, of course, pass resource to the rewrite directly...but the above is a more general solution that will work for any custom post type. 当然,您可以直接将resource传递给重写...但是以上是更通用的解决方案,适用于任何自定义帖子类型。

Note: You'll likely have to head to the WP-Admin and re-save your permalinks to properly "flush" them, in order for this to work. 注意:您可能必须前往WP-Admin并重新保存您的永久链接才能正确“刷新”它们,以使其正常工作。

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

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