I am trying to show the category name in a wordpress loop for the category page of a custom taxonomy and I have done many searches for an answer and I have not been able to find it or I am doing something wrong. I am trying to show only 1 term. The categories have sub categories and another level of subcategories as well.
So I need the term to dynamically reflect the term of page that is being viewed. I have this code that works but it displays the whole list of terms associated with the post and since the post has parent, child, and granchild terms, I don’t want it to show all the terms at once.
<?php $terms = wp_get_post_terms( $post->ID, 'videoscategory');
echo '<h2>';
foreach ($terms as $term) {
echo '<a href="'.get_term_link($term->slug, 'videoscategory').'">'.$term->name.'</a>';
}
echo '</h2>'; ?>
This has to do with the
foreach
, which is looping through every term. A quick and dirty way to do it is to insert<?php break; ?>
before theendfor
See this thread for more: How to get only one result from foreach(PHP)