(Revised on March 7th 2020 UTC)
因此共享相同的永久链接路径(或
rewrite slug/base 如中所示
example.com/<rewrite slug>/<term slug>
和
example.com/<rewrite slug>/<post slug>
) 是可能的,下面是(更新的)代码,您可以共享
the exact same rewrite slug 在分类法和帖子类型之间:
function wpse_358157_parse_request( $wp ) {
$path = \'about-us/video-center\'; // rewrite slug; no trailing slashes
$taxonomy = \'video-category\'; // taxonomy slug
$post_type = \'video\'; // post type slug
if ( preg_match( \'#^\' . preg_quote( $path, \'#\' ) . \'/#\', $wp->request ) &&
isset( $wp->query_vars[ $taxonomy ] ) ) {
$slug = $wp->query_vars[ $taxonomy ];
$slug = ltrim( substr( $slug, strrpos( $slug, \'/\' ) ), \'/\' );
if ( ! term_exists( $slug, $taxonomy ) ) {
$wp->query_vars[\'name\'] = $wp->query_vars[ $taxonomy ];
$wp->query_vars[\'post_type\'] = $post_type;
$wp->query_vars[ $post_type ] = $wp->query_vars[ $taxonomy ];
unset( $wp->query_vars[ $taxonomy ] );
}
}
}
add_action( \'parse_request\', \'wpse_358157_parse_request\' );
简要说明:
通过slug检查一个术语是否存在(比检查一个post是否存在)要容易得多,因此在上面的代码中,我们检查当前请求是否满足一个术语请求(即分类查询变量-例如。
video-category
— 存在于请求中,并且请求路径与分类法和post类型的重写slug匹配,如果是,则术语
doesn\'t exist, 然后我们假设这是一个post请求。
一、 我们让WordPress将该请求视为一个帖子请求,而不是一个术语。
其他注意事项请确保您的帖子和术语不存在相同的问题。因为帖子和术语共享相同的重写段,所以无法知道我们应该加载帖子还是术语。
在最初的回答中,我提到分类法/术语permalink不能是分层的(例如。example.com/<rewrite slug>/<parent term slug>/<child term slug>/
), 但实际上,它可以是分层的!我只是没有时间彻底测试一下……)
register_taxonomy( \'video-category\', \'video\', [
\'rewrite\' => [
\'slug\' => \'about-us/video-center\',
\'with_front\' => false,
\'hierarchical\' => true, // enable hierarchical term permalink
],
\'hierarchical\' => true,
...
]
);
我还说过,post类型需要在分类法之前注册,但在我最近的测试中,事实恰恰相反——我先注册了分类法。
因此,请尝试先注册分类法,然后注册post类型。如果这不起作用,那么首先注册post类型。
完整示例代码
function my_register_taxonomy() {
register_taxonomy( \'video-category\', \'video\', [
\'rewrite\' => [
\'slug\' => \'about-us/video-center\',
\'with_front\' => false,
\'hierarchical\' => true,
],
\'hierarchical\' => true,
\'labels\' => [
\'name\' => \'Video Categories\',
\'singular_name\' => \'Video Category\',
],
\'show_in_rest\' => true,
]
);
}
function my_register_post_type() {
register_post_type( \'video\', [
\'rewrite\' => [
\'slug\' => \'about-us/video-center\',
\'with_front\' => false,
],
\'has_archive\' => \'about-us/video-center\',
\'hierarchical\' => true,
\'public\' => true,
\'supports\' => [ \'title\', \'editor\', \'page-attributes\' ],
\'labels\' => [
\'name\' => \'Videos\',
\'singular_name\' => \'Video\',
],
\'show_in_rest\' => true,
]
);
}
add_action( \'init\', function () {
my_register_taxonomy();
my_register_post_type();
/* If that doesn\'t work, you can try switching them:
my_register_post_type();
my_register_taxonomy();
*/
} );
function wpse_358157_parse_request( $wp ) {
$path = \'about-us/video-center\'; // rewrite slug; no trailing slashes
$taxonomy = \'video-category\'; // taxonomy slug
$post_type = \'video\'; // post type slug
if ( preg_match( \'#^\' . preg_quote( $path, \'#\' ) . \'/#\', $wp->request ) &&
isset( $wp->query_vars[ $taxonomy ] ) ) {
$slug = $wp->query_vars[ $taxonomy ];
$slug = ltrim( substr( $slug, strrpos( $slug, \'/\' ) ), \'/\' );
if ( ! term_exists( $slug, $taxonomy ) ) {
$wp->query_vars[\'name\'] = $wp->query_vars[ $taxonomy ];
$wp->query_vars[\'post_type\'] = $post_type;
$wp->query_vars[ $post_type ] = $wp->query_vars[ $taxonomy ];
unset( $wp->query_vars[ $taxonomy ] );
}
}
}
add_action( \'parse_request\', \'wpse_358157_parse_request\' );