0

i have one cpt Anime and it has post_type taxonomy with term anime and animation

anime(cpt): (site domain)/anime/post title
     |
     |-(tax)Anime
     |-(tax)Animation

And my url structure are like: (site domain)/anime/post title but i wanna this kind of urls:

anime
     |
     |-(tax)Anime: (site domain)/anime/post title
     |-(tax)Animation: (site domain)/animation/post title

i really don't know how to build it.

after alot of try and error i find my solution:

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);  
 }

but now i face a new problem, my old urls still working like:

http://animup.net/anime/the-advantures-of-brer-rabbit/ (should redirect to) http://animup.net/animation/the-advantures-of-brer-rabbit/

0

The old permalinks still work because the rewrite rules that support those permalinks still exist. When you register a post type, setting the rewrite argument generates the rules to resolve incoming requests. Rather than adding new rules in addition to the originals, just alter the original post type registration.

| improve this answer | |
  • i don't wanna redirect all urls, i only wanna redirect posts with animation taxonomy term. – user1489083 Mar 18 '15 at 16:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.