这里的想法是使用查询对象的ID来获取post术语(,因为我们很可能在这里的循环之外,如果在循环内部,只需使用get_the_ID()
)。从那里,我们可以使用wp_list_pluck()
获取所有术语ID并将其传递给get_terms()
exclude
参数
只是一个注释,如WordPress V4。5,分类法应作为$args
, 我会处理这两个案子
4.5之前
// Set all our variables
$taxonomy = \'category\';
$post_id = $GLOBALS[\'wp_the_query\']->get_queried_object_id();
$args = [
\'hide_empty\' => false
];
// Get the ID\'s from the post terms
$post_terms = get_the_terms( $post_id, $taxonomy );
if ( $post_terms
&& !is_wp_error( $post_terms )
) {
$term_ids = wp_list_pluck( $post_terms, \'term_id\' );
// Get all the terms with the post terms excluded
$args[\'exclude\'] = $term_ids;
}
$terms = get_terms( $taxonomy, $args );
V 4.5+版本
// Set all our variables
$taxonomy = \'category\';
$post_id = $GLOBALS[\'wp_the_query\']->get_queried_object_id();
$args = [
\'taxonomy\' => $taxonomy,
\'hide_empty\' => false
];
// Get the ID\'s from the post terms
$post_terms = get_the_terms( $post_id, $taxonomy );
if ( $post_terms
&& !is_wp_error( $post_terms )
) {
$term_ids = wp_list_pluck( $post_terms, \'term_id\' );
// Get all the terms with the post terms excluded
$args[\'exclude\'] = $term_ids;
}
$terms = get_terms( $args );