没有通过插入排除的选项get_terms()
. 您需要根据术语的slug获取所需的ID,然后排除这些ID,如Pabamato的回答所示。
但如果在输出时跳过它们,而不是使用exclude
, 或进行其他查询。
$count_roles = count( $roles );
if ( $count_roles > 0 ) :
$exclude = [\'graduate\', \'job-market-candidate\', \'graduate-student\',\'research\'];
foreach ( $roles as $role ) {
if ( ! in_array( $role->slug, $exclude ) ) {
continue;
}
// Do stuff.
}
endif;
或者,您可以在检索结果集中的相关术语后,使用
array_filter()
, 然后继续正常操作。
$exclude = [\'graduate\', \'job-market-candidate\', \'graduate-student\',\'research\'];
$roles = get_terms( array(
\'taxonomy\' => \'role\',
\'orderby\' => \'ID\',
\'order\' => \'ASC\',
\'hide_empty\' => true,
) );
$roles = array_filter( $roles, function( $role ) {
return in_array( $role->slug, $exclude ) ? false : true;
} );
$count_roles = count( $roles );