我不久前写过这个函数,它可能有点马虎,但它确实做到了这一点:
function get_low_level_term( $taxonomy ) {
$term = null;
if( is_single() ) {
global $post;
$terms = get_the_terms( $post->ID, $taxonomy );
if( count( $terms ) == 1 ) {
foreach( $terms as $t ) {
$term = $t;
}
}
else {
foreach( $terms as $t ) {
if( $t->parent ) {
$term = $t;
}
}
if( !$term ) {
$count = 0;
foreach( $terms as $t ) {
$term = $count == 0 ? $t : $term;
$count ++;
}
}
}
}
return $term;
}