admin管理员组文章数量:1130349
I have
custom post type -> resources
custom taxonomy -> resources_type
I want custom permalinks where category base will be appended at the end instead of front
For example: I add a new resource with title "First resource" and assign term as type1
What I get -
What i want -
I am using the following argument code in register post type :
'rewrite' => array("slug" => "/resources/resources-type/%resources_type%"),
And then using following code to replace the "%resources_type%" in above code
function wpa_course_post_link( $post_link, $id = 0 ){
$post = get_post($id);
if ( is_object( $post ) ){
$terms = wp_get_object_terms( $post->ID, 'resources_type' );
if( $terms ){
return str_replace( '%resources_type%' , $terms[0]->slug , $post_link );
}
}
return $post_link;
}
add_filter( 'post_type_link', 'wpa_course_post_link', 1, 3 );
I have
custom post type -> resources
custom taxonomy -> resources_type
I want custom permalinks where category base will be appended at the end instead of front
For example: I add a new resource with title "First resource" and assign term as type1
What I get - http://example/resources/resources-type/type1/first-resource
What i want - http://example/resources/first-resource/resources-type/type1
I am using the following argument code in register post type :
'rewrite' => array("slug" => "/resources/resources-type/%resources_type%"),
And then using following code to replace the "%resources_type%" in above code
function wpa_course_post_link( $post_link, $id = 0 ){
$post = get_post($id);
if ( is_object( $post ) ){
$terms = wp_get_object_terms( $post->ID, 'resources_type' );
if( $terms ){
return str_replace( '%resources_type%' , $terms[0]->slug , $post_link );
}
}
return $post_link;
}
add_filter( 'post_type_link', 'wpa_course_post_link', 1, 3 );
Share
Improve this question
edited Nov 11, 2018 at 16:26
user7459842
asked Nov 11, 2018 at 12:16
user7459842user7459842
721 silver badge10 bronze badges
1 Answer
Reset to default 1 +50You can achieve the following permalink structure by simply overriding (without having to remove) the default one (you specified) for the resources post type:
/resources/%resources%/resources-type/%resources_type% <- preferred
/resources/resources-type/%resources_type%/%resources% <- default
And you can override it via add_permastruct() — first, register the post type without specifying the rewrite parameter which means the rewrite defaults to true/enabled and that the slug would be resources (i.e. the post type key or slug):
register_post_type( 'resources', array(
'label' => 'Resources',
'public' => true,
// no need to set the 'rewrite' arg
...
) );
Then right after that, override the permalink structure like so:
add_permastruct( 'resources', '/resources/%resources%/resources-type/%resources_type%' );
where the format is — the important part is the {POST TYPE KEY}, which in your case is resources:
add_permastruct( '{POST TYPE KEY}', '{PERMALINK STRUCTURE}' );
(Don't forget to flush the rewrite rules — just visit the permalink settings page.)
How to disable /resources/%resources%
/resources/%resources% (i.e. /{POST TYPE KEY}/%{POST TYPE KEY}%) is automatically added if %{POST TYPE KEY}% is found in the permalink structure. You can remove it like so:
add_filter( 'resources_rewrite_rules', 'filter_resources_rewrite_rules' );
function filter_resources_rewrite_rules( $rules ) {
$pattern = 'resources/([^/]+)/resources-type/';
$p_length = strlen( $pattern );
foreach ( $rules as $regex => $query ) {
if ( $pattern !== substr( $regex, 0, $p_length ) ) {
unset( $rules[ $regex ] );
}
}
return $rules;
}
If the post type is hierarchical, then the $pattern would be resources/(.+?)/resources-type/.
And if you'd like to use the above code/function with another post type, you basically only need to replace the text "resources" with the proper post type key.
See https://developer.wordpress/reference/hooks/permastructname_rewrite_rules/ for more details on that filter.
I have
custom post type -> resources
custom taxonomy -> resources_type
I want custom permalinks where category base will be appended at the end instead of front
For example: I add a new resource with title "First resource" and assign term as type1
What I get -
What i want -
I am using the following argument code in register post type :
'rewrite' => array("slug" => "/resources/resources-type/%resources_type%"),
And then using following code to replace the "%resources_type%" in above code
function wpa_course_post_link( $post_link, $id = 0 ){
$post = get_post($id);
if ( is_object( $post ) ){
$terms = wp_get_object_terms( $post->ID, 'resources_type' );
if( $terms ){
return str_replace( '%resources_type%' , $terms[0]->slug , $post_link );
}
}
return $post_link;
}
add_filter( 'post_type_link', 'wpa_course_post_link', 1, 3 );
I have
custom post type -> resources
custom taxonomy -> resources_type
I want custom permalinks where category base will be appended at the end instead of front
For example: I add a new resource with title "First resource" and assign term as type1
What I get - http://example/resources/resources-type/type1/first-resource
What i want - http://example/resources/first-resource/resources-type/type1
I am using the following argument code in register post type :
'rewrite' => array("slug" => "/resources/resources-type/%resources_type%"),
And then using following code to replace the "%resources_type%" in above code
function wpa_course_post_link( $post_link, $id = 0 ){
$post = get_post($id);
if ( is_object( $post ) ){
$terms = wp_get_object_terms( $post->ID, 'resources_type' );
if( $terms ){
return str_replace( '%resources_type%' , $terms[0]->slug , $post_link );
}
}
return $post_link;
}
add_filter( 'post_type_link', 'wpa_course_post_link', 1, 3 );
Share
Improve this question
edited Nov 11, 2018 at 16:26
user7459842
asked Nov 11, 2018 at 12:16
user7459842user7459842
721 silver badge10 bronze badges
1 Answer
Reset to default 1 +50You can achieve the following permalink structure by simply overriding (without having to remove) the default one (you specified) for the resources post type:
/resources/%resources%/resources-type/%resources_type% <- preferred
/resources/resources-type/%resources_type%/%resources% <- default
And you can override it via add_permastruct() — first, register the post type without specifying the rewrite parameter which means the rewrite defaults to true/enabled and that the slug would be resources (i.e. the post type key or slug):
register_post_type( 'resources', array(
'label' => 'Resources',
'public' => true,
// no need to set the 'rewrite' arg
...
) );
Then right after that, override the permalink structure like so:
add_permastruct( 'resources', '/resources/%resources%/resources-type/%resources_type%' );
where the format is — the important part is the {POST TYPE KEY}, which in your case is resources:
add_permastruct( '{POST TYPE KEY}', '{PERMALINK STRUCTURE}' );
(Don't forget to flush the rewrite rules — just visit the permalink settings page.)
How to disable /resources/%resources%
/resources/%resources% (i.e. /{POST TYPE KEY}/%{POST TYPE KEY}%) is automatically added if %{POST TYPE KEY}% is found in the permalink structure. You can remove it like so:
add_filter( 'resources_rewrite_rules', 'filter_resources_rewrite_rules' );
function filter_resources_rewrite_rules( $rules ) {
$pattern = 'resources/([^/]+)/resources-type/';
$p_length = strlen( $pattern );
foreach ( $rules as $regex => $query ) {
if ( $pattern !== substr( $regex, 0, $p_length ) ) {
unset( $rules[ $regex ] );
}
}
return $rules;
}
If the post type is hierarchical, then the $pattern would be resources/(.+?)/resources-type/.
And if you'd like to use the above code/function with another post type, you basically only need to replace the text "resources" with the proper post type key.
See https://developer.wordpress/reference/hooks/permastructname_rewrite_rules/ for more details on that filter.
本文标签: Custom permalinkappend taxonomy name and term name at the end of permalink
版权声明:本文标题:Custom permalink - append taxonomy name and term name at the end of permalink 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749167811a2326486.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论