admin管理员组文章数量:1130349
I am having trouble working with WordPress rewrite rules, and could use some help.
I have a custom post type called _shows_.
All shows have a single custom taxonomy category _show-category_. A _show_ will never have more than one _show-category_.
I would like my urls to route in this manner:
www.mysite/shows/ => archive-shows.php
www.mysite/shows/%category%/ => taxonomy-show-category.php
www.mysite/shows/%category%/%postname%/ => single-shows.php
So as a real world example, lets say we have a _show-category_ "Foo" and a _show_ post titled "Bar" that has "Foo" as it's _show-category_. I would expect my WordPress app to look like this:
www.mysite/shows/foo/ => shows all posts under the foo category
www.mysite/shows/foo/bar => shows the indivual post
I try to avoid plugins when possible, but am open to any solution.
I am having trouble working with WordPress rewrite rules, and could use some help.
I have a custom post type called _shows_.
All shows have a single custom taxonomy category _show-category_. A _show_ will never have more than one _show-category_.
I would like my urls to route in this manner:
www.mysite/shows/ => archive-shows.php
www.mysite/shows/%category%/ => taxonomy-show-category.php
www.mysite/shows/%category%/%postname%/ => single-shows.php
So as a real world example, lets say we have a _show-category_ "Foo" and a _show_ post titled "Bar" that has "Foo" as it's _show-category_. I would expect my WordPress app to look like this:
www.mysite/shows/foo/ => shows all posts under the foo category
www.mysite/shows/foo/bar => shows the indivual post
I try to avoid plugins when possible, but am open to any solution.
Share Improve this question edited Jul 28, 2016 at 6:58 user9447 1,7927 gold badges30 silver badges55 bronze badges asked Aug 1, 2013 at 16:16 Paul TPaul T 6671 gold badge6 silver badges7 bronze badges 2- 1 just pointing you to something i had to do before.. wordpress.stackexchange/questions/102246/… – reikyoushin Commented Aug 1, 2013 at 17:14
- Brilliant question, and the accepted answer by @Milo is so well done it really should be in official Wordpress documentation. This is a VERY common use case for proper URLs. – Khom Nazid Commented Apr 6, 2022 at 1:36
1 Answer
Reset to default 93First, register your taxonomy and set the slug argument of rewrite to shows:
register_taxonomy(
'show_category',
'show',
array(
'rewrite' => array( 'slug' => 'shows', 'with_front' => false ),
// your other args...
)
);
Next, register your post type and set the slug to shows/%show_category%, and set has_archive argument to shows:
register_post_type(
'show',
array(
'rewrite' => array( 'slug' => 'shows/%show_category%', 'with_front' => false ),
'has_archive' => 'shows',
// your other args...
)
);
Last, add a filter to post_type_link to substitute the show category in individual show permalinks:
function wpa_show_permalinks( $post_link, $post ){
if ( is_object( $post ) && $post->post_type == 'show' ){
$terms = wp_get_object_terms( $post->ID, 'show_category' );
if( $terms ){
return str_replace( '%show_category%' , $terms[0]->slug , $post_link );
}
}
return $post_link;
}
add_filter( 'post_type_link', 'wpa_show_permalinks', 1, 2 );
EDIT
Forgot the has_archive argument of register_post_type above, that should be set to shows.
I am having trouble working with WordPress rewrite rules, and could use some help.
I have a custom post type called _shows_.
All shows have a single custom taxonomy category _show-category_. A _show_ will never have more than one _show-category_.
I would like my urls to route in this manner:
www.mysite/shows/ => archive-shows.php
www.mysite/shows/%category%/ => taxonomy-show-category.php
www.mysite/shows/%category%/%postname%/ => single-shows.php
So as a real world example, lets say we have a _show-category_ "Foo" and a _show_ post titled "Bar" that has "Foo" as it's _show-category_. I would expect my WordPress app to look like this:
www.mysite/shows/foo/ => shows all posts under the foo category
www.mysite/shows/foo/bar => shows the indivual post
I try to avoid plugins when possible, but am open to any solution.
I am having trouble working with WordPress rewrite rules, and could use some help.
I have a custom post type called _shows_.
All shows have a single custom taxonomy category _show-category_. A _show_ will never have more than one _show-category_.
I would like my urls to route in this manner:
www.mysite/shows/ => archive-shows.php
www.mysite/shows/%category%/ => taxonomy-show-category.php
www.mysite/shows/%category%/%postname%/ => single-shows.php
So as a real world example, lets say we have a _show-category_ "Foo" and a _show_ post titled "Bar" that has "Foo" as it's _show-category_. I would expect my WordPress app to look like this:
www.mysite/shows/foo/ => shows all posts under the foo category
www.mysite/shows/foo/bar => shows the indivual post
I try to avoid plugins when possible, but am open to any solution.
Share Improve this question edited Jul 28, 2016 at 6:58 user9447 1,7927 gold badges30 silver badges55 bronze badges asked Aug 1, 2013 at 16:16 Paul TPaul T 6671 gold badge6 silver badges7 bronze badges 2- 1 just pointing you to something i had to do before.. wordpress.stackexchange/questions/102246/… – reikyoushin Commented Aug 1, 2013 at 17:14
- Brilliant question, and the accepted answer by @Milo is so well done it really should be in official Wordpress documentation. This is a VERY common use case for proper URLs. – Khom Nazid Commented Apr 6, 2022 at 1:36
1 Answer
Reset to default 93First, register your taxonomy and set the slug argument of rewrite to shows:
register_taxonomy(
'show_category',
'show',
array(
'rewrite' => array( 'slug' => 'shows', 'with_front' => false ),
// your other args...
)
);
Next, register your post type and set the slug to shows/%show_category%, and set has_archive argument to shows:
register_post_type(
'show',
array(
'rewrite' => array( 'slug' => 'shows/%show_category%', 'with_front' => false ),
'has_archive' => 'shows',
// your other args...
)
);
Last, add a filter to post_type_link to substitute the show category in individual show permalinks:
function wpa_show_permalinks( $post_link, $post ){
if ( is_object( $post ) && $post->post_type == 'show' ){
$terms = wp_get_object_terms( $post->ID, 'show_category' );
if( $terms ){
return str_replace( '%show_category%' , $terms[0]->slug , $post_link );
}
}
return $post_link;
}
add_filter( 'post_type_link', 'wpa_show_permalinks', 1, 2 );
EDIT
Forgot the has_archive argument of register_post_type above, that should be set to shows.
本文标签: Permalinks custom post type gt custom taxonomy gt post
版权声明:本文标题:Permalinks: custom post type -> custom taxonomy -> post 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749242052a2338261.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论