在自定义帖子类型/分类法中向URL添加类别库
-
-
最近,我面临这个问题.**已解决!** [#188834] [1] [1]:http://wordpress.stackexchange.com/questions/94817/add-category-base-to-url-in-custom-post-type-taxonomy/188834#188834Recently, I face this issue. **Solved!** [#188834][1] [1]: http://wordpress.stackexchange.com/questions/94817/add-category-base-to-url-in-custom-post-type-taxonomy/188834#188834
- 0
- 2015-05-20
- maheshwaghmare
-
**解决方案!**(经过无休止的研究)
,您应该修改`post_type_link`过滤器.更多信息,请访问:http://wordpress.stackexchange.com/a/167992/33667)**SOLUTION!** (After endless research)
you should modify the `post_type_link` filter. more at: http://wordpress.stackexchange.com/a/167992/33667 )- 0
- 2015-07-26
- T.Todua
-
[如何更改自定义帖子类型的永久链接结构及其分类法?](http://wordpress.stackexchange.com/questions/28979/how-to-change-permalink-structure-for-custom-post-type的可能重复项及其分类法)possible duplicate of [How to change permalink structure for custom post type and it's taxonomies?](http://wordpress.stackexchange.com/questions/28979/how-to-change-permalink-structure-for-custom-post-type-and-its-taxonomies)
- 0
- 2015-07-26
- T.Todua
-
9 个回答
- 投票数
-
- 2013-04-06
更改您的重写以添加课程查询var:
'rewrite' => array('slug' => 'courses/%course%')
然后过滤器
post_type_link
将所选课程插入永久链接:function wpa_course_post_link( $post_link, $id = 0 ){ $post = get_post($id); if ( is_object( $post ) ){ $terms = wp_get_object_terms( $post->ID, 'course' ); if( $terms ){ return str_replace( '%course%' , $terms[0]->slug , $post_link ); } } return $post_link; } add_filter( 'post_type_link', 'wpa_course_post_link', 1, 3 );
还有像自定义帖子类型永久链接这样的插件可以为您完成此操作.
Change your rewrite to add the course query var:
'rewrite' => array('slug' => 'courses/%course%')
Then filter
post_type_link
to insert the selected course into the permalink:function wpa_course_post_link( $post_link, $id = 0 ){ $post = get_post($id); if ( is_object( $post ) ){ $terms = wp_get_object_terms( $post->ID, 'course' ); if( $terms ){ return str_replace( '%course%' , $terms[0]->slug , $post_link ); } } return $post_link; } add_filter( 'post_type_link', 'wpa_course_post_link', 1, 3 );
There are also plugins like Custom Post Type Permalinks that can do this for you.
-
谢谢,感谢您的迅速答复.这完全有道理.不过我很好奇,我应该在哪里插入过滤器post_type_link?我可以只浏览整个文档的底部吗?Thank you, I appreciate your swift answer. This makes complete sense. I'm curious though, where do I insert the filter post_type_link at? can i just go at the bottom of the entire document?
- 0
- 2013-04-06
- Zach Russell
-
我将其添加到底部,它是404的页面.I added it to the bottom and it 404's the page.
- 0
- 2013-04-06
- Zach Russell
-
您必须清除重写,请访问永久链接设置页面.you have to flush rewrites, visit the permalinks settings page.
- 1
- 2013-04-06
- Milo
-
还请注意,您可能会与分类法和帖子类型发生冲突,两者均共享相同的标签.also note you'll likely have a clash with a taxonomy and a post type both sharing the same slug.
- 0
- 2013-04-06
- Milo
-
我现在所处的位置是正确的永久链接,但执行不正确(它是软404ing).关于如何使此工作正常的任何建议?我没有永久链接刷新重写.只需单击"保存",它就会更新文件(它是nginx,因此在nginx.conf文件中进行了控制)Where i'm at now is it's making the permalinks right, but it's not executing correctly (it's soft 404ing). Any recommendations on what I can do to get this working right? I'm away of the permalink flush rewrites. Just click 'save' and it updates the file (it's nginx so it's controlled in the nginx.conf file)
- 0
- 2013-04-06
- Zach Russell
-
得到它了!这是一个冲突.Got it! It was a conflict.
- 0
- 2013-04-06
- Zach Russell
-
下载-存档页面404Downvote - archive page 404
- 1
- 2020-04-04
- Dev
-
这不适用于多语言网站上的翻译帖子(例如使用WPML).This doesn't work for translated posts on a multilingual site (for example using WPML).
- 0
- 2020-08-05
- montrealist
-
这将在新创建的URL上为我显示首页.您必须使用在@Floris的答案中看到的add_rewrite_rule()使其起作用.另请注意,存档页面停止工作,我的/my-post-type和/my-post-type/my-category都显示404This shows frontpage for me on the newly created URL. You gotta use the add_rewrite_rule() seen in @Floris's answer to make it work. Also note, archive pages stop working, I get 404 for /my-post-type and for /my-post-type/my-category
- 0
- 2020-09-01
- trainoasis
-
- 2019-02-11
对我来说,解决方案包括三个部分.在我的情况下,帖子类型称为
trainings
.- 添加
'rewrite' => array('slug' => 'trainings/%cat%')
到register_post_type
函数. - 将子弹更改为具有动态类别.
- "收听"新的动态URL并加载适当的模板.
因此,这是如何动态更改给定帖子类型的永久链接.添加到
functions.php
:function vx_soon_training_post_link( $post_link, $id = 0 ) { $post = get_post( $id ); if ( is_object( $post ) ) { $terms = wp_get_object_terms( $post->ID, 'training_cat' ); if ( $terms ) { return str_replace( '%cat%', $terms[0]->slug, $post_link ); } } return $post_link; } add_filter( 'post_type_link', 'vx_soon_training_post_link', 1, 3 );
...这是如何在新的动态URL上加载适当的模板.添加到
functions.php
:function archive_rewrite_rules() { add_rewrite_rule( '^training/(.*)/(.*)/?$', 'index.php?post_type=trainings&name=$matches[2]', 'top' ); //flush_rewrite_rules(); // use only once } add_action( 'init', 'archive_rewrite_rules' );
就这样!请记住通过在后端再次保存永久链接来刷新永久链接.或使用
flush_rewrite_rules()
函数.The solution for me had three parts. In my case the post type is called
trainings
.- Add
'rewrite' => array('slug' => 'trainings/%cat%')
to theregister_post_type
function. - Change the slug to have a dynamic category.
- "Listen" to the new dynamic URL and load the appropriate template.
So here is how to change the permalink dynamically for a given post type. Add to
functions.php
:function vx_soon_training_post_link( $post_link, $id = 0 ) { $post = get_post( $id ); if ( is_object( $post ) ) { $terms = wp_get_object_terms( $post->ID, 'training_cat' ); if ( $terms ) { return str_replace( '%cat%', $terms[0]->slug, $post_link ); } } return $post_link; } add_filter( 'post_type_link', 'vx_soon_training_post_link', 1, 3 );
...and this is how to load the appropriate template on the new dynamic URL. Add to
functions.php
:function archive_rewrite_rules() { add_rewrite_rule( '^training/(.*)/(.*)/?$', 'index.php?post_type=trainings&name=$matches[2]', 'top' ); //flush_rewrite_rules(); // use only once } add_action( 'init', 'archive_rewrite_rules' );
Thats it! Remember to refresh the permalinks by saving the permalinks again in de backend. Or use the
flush_rewrite_rules()
function. -
- 2017-10-06
解决了!
要具有用于自定义帖子类型的层次结构永久链接,请安装"自定义帖子类型永久链接"( https://wordpress.org/plugins/custom-post-type-permalinks/)插件.
更新注册的帖子类型.我有帖子类型的名称作为帮助中心
function help_centre_post_type(){ register_post_type('helpcentre', array( 'labels' => array( 'name' => __('Help Center'), 'singular_name' => __('Help Center'), 'all_items' => __('View Posts'), 'add_new' => __('New Post'), 'add_new_item' => __('New Help Center'), 'edit_item' => __('Edit Help Center'), 'view_item' => __('View Help Center'), 'search_items' => __('Search Help Center'), 'no_found' => __('No Help Center Post Found'), 'not_found_in_trash' => __('No Help Center Post in Trash') ), 'public' => true, 'publicly_queryable'=> true, 'show_ui' => true, 'query_var' => true, 'show_in_nav_menus' => false, 'capability_type' => 'page', 'hierarchical' => true, 'rewrite'=> [ 'slug' => 'help-center', "with_front" => false ], "cptp_permalink_structure" => "/%help_centre_category%/%post_id%-%postname%/", 'menu_position' => 21, 'supports' => array('title','editor', 'thumbnail'), 'has_archive' => true )); flush_rewrite_rules(); } add_action('init', 'help_centre_post_type');
这是注册分类法
function themes_taxonomy() { register_taxonomy( 'help_centre_category', 'helpcentre', array( 'label' => __( 'Categories' ), 'rewrite'=> [ 'slug' => 'help-center', "with_front" => false ], "cptp_permalink_structure" => "/%help_centre_category%/", 'hierarchical' => true, 'public' => true, 'show_ui' => true, 'show_admin_column' => true, 'show_in_nav_menus' => true, 'query_var' => true ) ); } add_action( 'init', 'themes_taxonomy');
这行使您的永久链接正常工作
"cptp_permalink_structure" => "/%help_centre_category%/%post_id%-%postname%/",
您可以删除
%post_id%
并保留/%help_centre_category%/%postname%/"
别忘了从仪表板清除永久链接.
Got the solution!
To have hierarchical permalinks for custom post type install Custom Post Type Permalinks(https://wordpress.org/plugins/custom-post-type-permalinks/) plugin.
Update registered post type. I have post type name as help center
function help_centre_post_type(){ register_post_type('helpcentre', array( 'labels' => array( 'name' => __('Help Center'), 'singular_name' => __('Help Center'), 'all_items' => __('View Posts'), 'add_new' => __('New Post'), 'add_new_item' => __('New Help Center'), 'edit_item' => __('Edit Help Center'), 'view_item' => __('View Help Center'), 'search_items' => __('Search Help Center'), 'no_found' => __('No Help Center Post Found'), 'not_found_in_trash' => __('No Help Center Post in Trash') ), 'public' => true, 'publicly_queryable'=> true, 'show_ui' => true, 'query_var' => true, 'show_in_nav_menus' => false, 'capability_type' => 'page', 'hierarchical' => true, 'rewrite'=> [ 'slug' => 'help-center', "with_front" => false ], "cptp_permalink_structure" => "/%help_centre_category%/%post_id%-%postname%/", 'menu_position' => 21, 'supports' => array('title','editor', 'thumbnail'), 'has_archive' => true )); flush_rewrite_rules(); } add_action('init', 'help_centre_post_type');
And here is registered taxonomy
function themes_taxonomy() { register_taxonomy( 'help_centre_category', 'helpcentre', array( 'label' => __( 'Categories' ), 'rewrite'=> [ 'slug' => 'help-center', "with_front" => false ], "cptp_permalink_structure" => "/%help_centre_category%/", 'hierarchical' => true, 'public' => true, 'show_ui' => true, 'show_admin_column' => true, 'show_in_nav_menus' => true, 'query_var' => true ) ); } add_action( 'init', 'themes_taxonomy');
This is line makes your permalink work
"cptp_permalink_structure" => "/%help_centre_category%/%post_id%-%postname%/",
you can remove
%post_id%
and can keep/%help_centre_category%/%postname%/"
Don't forget to flush permalinks from dashboard.
-
- 2015-05-20
Yep! After a lot of research I got plugin 'Custom Permalinks'. Which fulfils my requirement regards - custom URL e.g.
- for Category
- for Post
- for Custom Post
- for Custom Taxonomy etc.
Like this Custom Post Type - Post:
-
- 2019-06-12
您需要在使用register_post_type函数注册自定义帖子类型的行下方更新.
'rewrite'=> array('slug'=>'courses/%cat%')
要动态更改帖子类型的永久链接,必须在functions.php文件中添加以下代码:
function change_link( $post_link, $id = 0 ) { $post = get_post( $id ); if( $post->post_type == 'courses' ) { if ( is_object( $post ) ) { $terms = wp_get_object_terms( $post->ID, array('course') ); if ( $terms ) { return str_replace( '%cat%', $terms[0]->slug, $post_link ); } } } return $post_link ; } add_filter( 'post_type_link', 'change_link', 1, 3 ); //load the template on the new generated URL otherwise you will get 404's the page function generated_rewrite_rules() { add_rewrite_rule( '^courses/(.*)/(.*)/?$', 'index.php?post_type=courses&name=$matches[2]', 'top' ); } add_action( 'init', 'generated_rewrite_rules' );
此后,您需要刷新重写永久链接,转到 wp-admin>设置>永久链接.只需使用"保存更改"按钮更新固定链接设置即可.
它将返回如下网址:
- domain.com/courses/[课程名称{category}]/课程名称
谢谢!
You need to update below line at where you have register a custom post type using register_post_type function.
'rewrite' => array('slug' => 'courses/%cat%')
To change permalink dynamically of post type you have to add below code in functions.php file :
function change_link( $post_link, $id = 0 ) { $post = get_post( $id ); if( $post->post_type == 'courses' ) { if ( is_object( $post ) ) { $terms = wp_get_object_terms( $post->ID, array('course') ); if ( $terms ) { return str_replace( '%cat%', $terms[0]->slug, $post_link ); } } } return $post_link ; } add_filter( 'post_type_link', 'change_link', 1, 3 ); //load the template on the new generated URL otherwise you will get 404's the page function generated_rewrite_rules() { add_rewrite_rule( '^courses/(.*)/(.*)/?$', 'index.php?post_type=courses&name=$matches[2]', 'top' ); } add_action( 'init', 'generated_rewrite_rules' );
After that, you need to flush rewrites permalinks, goto the wp-admin > Settings > permalinks. just update permalink setting using "Save Changes" button.
it'll return urls like below :
- domain.com/courses/[course-name{category}]/lesson-name
Thank you!
-
否决票-单一CPT和CTP存档页面类型均为404Down vote - 404 on both single CPT and CTP archive page types
- 0
- 2020-04-04
- Dev
-
@dev是否遵循我在回答中提到的所有步骤?您是否刷新了重写永久链接?我已经测试过,并且工作正常.@dev did you follow all steps which i have mentioned in my answer ? did you flush your rewrite permalinks? I have tested and it is working fine.
- 0
- 2020-04-04
- Chetan Vaghela
-
- 2017-05-03
这对我有用:
'rewrite' => array( 'slug' => 'portfolio', 'with_front' => false, 'hierarchical' => true // to display category/subcategroy ),
This is worked for me :
'rewrite' => array( 'slug' => 'portfolio', 'with_front' => false, 'hierarchical' => true // to display category/subcategroy ),
-
这不使用类别或它们的路径,仅使自定义帖子类型成为分层.This does not make use of the categories or their path it only makes the custom post type hierarchical.
- 5
- 2017-08-25
- Joris Kroos
-
- 2019-07-01
对于对解决方案感兴趣的任何人,而不必修改原始PHP代码,我强烈建议使用插件 Permalink Manager Lite .它可以拯救生命.
它具有可视化机制,可根据"永久结构"在自定义帖子类型的URL中删除或添加所需的任何部分:
(由于使用自定义帖子类型进行简单的URL结构涉及到所有痛苦,我们将放弃WP而转到另一个CMS.但是,此插件与ACF和CPTUI或Pods结合使用使Wordpress相当专业.)
To anyone interested in the solution, without having to tinker with raw PHP code, I highly recommend the plugin Permalink Manager Lite by Maciej Bis. It's a life saver.
It has a visual mechanism to remove or add whatever part you want in the custom post type's URL based on 'permastructs':
(With all the pain involved in simple URL structuring with custom post types, we were about to give up on WP and move to another CMS. But this plugin in conjunction with ACF and CPTUI or Pods makes Wordpress fairly professional.)
-
- 2020-05-29
如果您使用的是
get_post_type_archive_link()
,则可能需要使用/%cat%/
过滤器从网址中删除post_type_archive_link
.If you're using
get_post_type_archive_link()
, maybe you will need to remove/%cat%/
from the URL usingpost_type_archive_link
filter. -
- 2020-08-18
我发现@ chetan-vaghela的答案几乎是完美的;在我的用例中,我还希望能够看到该帖子类型的所有帖子列表,例如典型的存档页面(即/courses/,后面没有任何分类法).我只需要添加一个其他重写规则,如下所示:
function generated_rewrite_rules() { add_rewrite_rule( '^courses/(.*)/(.*)/?$', 'index.php?post_type=courses&name=$matches[2]', 'top' ); }
I found @chetan-vaghela 's answer almost perfect; in my use case I also wanted to be able to see a list of all posts by this post type like a typical archive page (i.e. /courses/, without any taxonomy after it). I just had to add one additional rewrite rule as follows:
function generated_rewrite_rules() { add_rewrite_rule( '^courses/(.*)/(.*)/?$', 'index.php?post_type=courses&name=$matches[2]', 'top' ); }
我正在WordPress中构建由
自定义帖子类型
控制的LMS类型系统.帖子类型称为<课程>课程(其中包含<课程>课程一小段),并且具有一种<课程>自定义分类法(分类)课程>(类别),称为<课程>课程.
域url结构现在显示为:
domain.com/courses/课程名称
.我希望它成为:
domain.com/courses/[课程名称{category}]/课程名称
或本质上:
/[cpt]/%category%/%postname%/
这是我写的现在正在控制
CPT
的插件.