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