So I have custom taxonomy called "cars". It's an hierarchic taxonomy. Now what I want to do is print one term name on my single post page.
Post "A" has term "audi" and term audi has child "a4" and a4 has child term "DSI". Post is set to all these 3 terms. Now what I want to do is print in my single template is is lowest term in hierarchy. In this case it would be "DSI".
But then comes example 2.
Post "B" has term "bwm" only. Now what I want to do is print in my single template is is lowest term in hierarchy. In this case it would be "bmws".
How can I achieve this? When I get terms with wp_get_post_terms function, the terms are in random order. If I take the last term of array it can be any terms I have set the post in. Currently I have this code which obviously doesn't work:
$terms = wp_get_post_terms( get_the_ID(), 'cars', array("fields" => "all") ); foreach ( $terms as $term ) : if ( 0 != $term->parent ) : $term_name = $term->name; else : $parent_term_id = $term->term_id; endif; endforeach; echo '<h2>' . $term_name . '</h2>';