经过多次尝试和错误,我找到了解决方案:
add_filter(\'post_type_link\', \'events_permalink_structure\', 10, 4);
function events_permalink_structure($post_link, $post, $leavename, $sample){
$types=wp_get_object_terms( $post->ID, \'post_types\' );
if (( \'animation\' == $types[0]->slug )or ( \'comic\' == $types[0]->slug )) {
$post_link = str_replace( array(\'/anime/\',\'/manga/\'), \'/\'.$types[0]->slug.\'/\', $post_link );
}
return $post_link;
}
add_action(\'init\',\'animation_init\');
function animation_init() {
global $wp,$wp_rewrite,$wp_query;
$wp_rewrite->add_rule(\'^comic/([^/]*)/?$\',\'index.php?post_type=manga&name=$matches[1]\', \'top\');
$wp_rewrite->add_rule(\'^animation/([^/]*)/?$\',\'index.php?post_type=animes&name=$matches[1]\', \'top\');
// Once you get working, remove this next line
//$wp_rewrite->flush_rules(false);
}
但现在我面临一个新问题,我的旧URL仍然像这样工作:
http://animup.net/anime/the-advantures-of-brer-rabbit/(应重定向到)http://animup.net/animation/the-advantures-of-brer-rabbit/