我有 2 个自定义分类法,它们共享相同的 slug,并有一个自定义帖子,名为 Courses
1:
'rewrite' => [ 'slug' => 'courses/%tag%', 'with-front' => false ],
'labels' => [ 'name' => 'Tag' ]
2:
'rewrite' => [ 'slug' => 'courses/%cat%', 'with-front' => 'false' ],
'labels' => [ 'name' => 'Category' ]
我用 Categorytaxonomy 重写了自定义帖子类型的 URL,以便我可以拥有此 URL
courses/math/my-post
它有效,即使当我去学习课程/数学时,我也会看到我所有的数学帖子。
courses/math
但是我也有标签,我希望它们与类别处于同一级别,但我得到 404,例如:
courses/logic
我怎样才能实现这两个分类法可以在同一水平上共存?
这是我对自定义帖子类型的重写规则。
function courses_post_link( $post_link, $id = 0 ){
$post = get_post($id);
if ( is_object( $post ) ){
$terms = wp_get_object_terms( $post->ID, 'course_category' );
if( $terms ){
return str_replace( '%cat%' , $terms[0]->slug , $post_link );
}
}
return $post_link;
}
add_filter( 'post_type_link', 'courses_post_link', 1, 3 );
function archive_rewrite_rules() {
add_rewrite_rule(
'^courses/(.*)/(.*)/?$',
'index.php?post_type=courses&name=$matches[2]',
'top'
);
}
已解决。
我使用了一个名为“永久链接定制器”的插件。
您的第二个分类有错误:
'rewrite' => [ 'slug' => 'courses/%cat%', 'with-front' => 'false' ],`
这是不正确的,因为您将“with-front”值作为字符串发送。它应该是一个布尔值,例如
false
就像你的第一个分类法:
'rewrite' => [ 'slug' => 'courses/%cat%', 'with-front' => false ],