Show only term text and link of custom post type category that is being viewed

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>'; ?>

Related posts

Leave a Reply

1 comment