0

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.

Example 1.

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>';
  • Maybe this solution can help you. – ClodClod91 Jan 7 '17 at 20:02
  • No help for so what ever. Just ends same result as what in my question already, which isn't what I'm looking for – user995317 Jan 12 '17 at 8:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.