具有基于分类术语两种不同URL结构的定制帖子类型

时间:2014-12-12 作者:user1489083

我有一部cpt动画,它有post\\u类型的分类法,有动画和动画两个术语

anime(cpt): (site domain)/anime/post title
     |
     |-(tax)Anime
     |-(tax)Animation
我的url结构是:(站点域)/动画/帖子标题,但我想要这种url:

anime
     |
     |-(tax)Anime: (site domain)/anime/post title
     |-(tax)Animation: (site domain)/animation/post title
我真的不知道如何建造它。

2 个回复
SO网友:Karun

请检查此插件https://wordpress.org/plugins/rewrite/

它说这个插件可以创建自定义重写,而无需接触PHP代码

SO网友:user1489083

经过多次尝试和错误,我找到了解决方案:

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/

结束

相关推荐