admin管理员组文章数量:1332345
I have a CPT named rad.
I have the following code to change the permalink from "site/rad/post" to "site/category/subcategory/post".
function remove_cpt_slug( $post_link, $post, $leavename ) {
// Define the CPT's name.
$post_type_name="rad";
// Get the inmediate category.
$categoryc=get_the_category( $post->id );
// Get the parent category.
if(!empty($categoryc))
$categoryp=get_term( $categoryc[0]->parent, 'category' );
if ( $post_type_name != $post->post_type || 'publish' != $post->post_status ) {
return $post_link;
}
if(!empty($categoryp->errors)) {
$post_link = str_replace( $post_type_name , $categoryc[0]->name, $post_link );
}
if(empty($categoryp->errors) && !empty($categoryp->name)) {
$post_link = str_replace( $post_type_name , $categoryp->name."/".$categoryc[0]->name, $post_link );
}
return $post_link;
}
add_filter( 'post_type_link', 'remove_cpt_slug', 10, 3 );
When I create a new post, it works great. However, when I visit the new permalink it has a 404 error.
Any idea of how could I fix it?
本文标签: custom post typesChanging CPT permalink
版权声明:本文标题:custom post types - Changing CPT permalink 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742326074a2453752.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论