没有简单的方法,因为这会导致冲突。这意味着您必须自己解决这些冲突,并实现一些逻辑,这将决定要显示什么。
以下是示例方法:
add_filter( \'term_link\', \'repair_categories_link\', 10, 3 );
add_action( \'parse_request\', \'repair_links\' );
add_action( \'pre_get_posts\', \'repair_query_post_type\' );
// First we have to modify links for categories
function repair_categories_link( $url, $term, $taxonomy ) {
if ( \'worksheets_category\' == $taxonomy ) {
$url = str_replace( site_url( \'/worksheets_category/\' ), get_post_type_archive_link( \'worksheets\' ), $url );
}
return $url;
}
// Then we have to solve conflicts - if given category exists, show category and not a post
function repair_links( $wp ) {
if ( array_key_exists( \'worksheets\', $wp->query_vars) && $wp->query_vars[\'worksheets\'] ) {
if ( term_exists($wp->query_vars[\'worksheets\'], \'worksheets_category\') ) {
$wp->query_vars[\'worksheets_category\'] = $wp->query_vars[\'worksheets\'];
unset( $wp->query_vars[\'worksheets\'] );
unset( $wp->query_vars[\'name\'] );
unset( $wp->query_vars[\'post_type\'] );
}
}
}
// And we have to fix post type
function repair_query_post_type( $query ) {
if ( ! is_admin() && $query->is_main_query() && get_query_var(\'worksheets_category\') ) {
$query->set( \'post_type\', \'worksheets\' );
}
}