最下位のカテゴリを取得

//最下層のカテゴリを取得
function get_term_descendants ( $post_id, $tax_name) {

$terms = get_the_terms( $post_id, $tax_name );

if ( empty( $terms )) return false;

$candidate = $terms;
$count = count( $terms );

if ( $count > 1 ):

foreach( $terms as $key => $term ):

foreach( $terms as $term2 ):

if ( term_is_ancestor_of( $term->term_id, $term2->term_id, $tax_name ) ) {

unset( $candidate[$key] );
break;

}

endforeach;

endforeach;

endif;

return $candidate;

}