以下是我的一个项目中的部分代码,用于为permalinks设置类似的结构(post类型和分类法存档的基段代码相同),请注意post类型和分类法的“has\\u archive”和“rewrite”参数的值:
add_action( \'init\', \'register_my_post_types\' );
function register_my_post_types() {
register_post_type( \'movie\',
array(
....
\'has_archive\' => \'movies\',
\'rewrite\' => array(
\'slug\' => \'movies/%mv_category%\',
\'with_front\' => false
),
\'taxonomies\' => array(
\'mv_category\',
),
)
);
register_taxonomy(
\'mv_category\',
array(
\'movie\'
),
array(
...
\'hierarchical\' => true,
\'rewrite\' => array(
\'slug\' => \'movies\',
\'with_front\' => false,
\'hierarchical\' => false
)
)
);
) // end of create_my_post_types function
add_filter(\'post_type_link\', \'filter_post_type_link\', 10, 2);
function filter_post_type_link($link, $post)
{
if ($post->post_type != \'movie\')
return $link;
if ($cats = get_the_terms($post->ID, \'mv_category\'))
$link = str_replace(\'%mv_category%\', array_pop($cats)->slug, $link);
return $link;
}
然后,您可以使用以下url访问电影帖子类型的“纪录片”类别:
site.com/movies/documentary/
“纪录片”类别的“电影A”将是:
site.com/movies/documentary/movie-a/
注意:在post类型之后注册分类很重要,因为顺序permalink重写规则是在WordPress中生成的。