How do I get only the first term of a custom post type.
I can get all – no problem. This what I am using to grab all of them

<?php foreach ($terms as $term) {echo '<a href="'.get_term_link($term->slug, 'sitecat').'">'.$term->name.'</a>,';} ?> >> <a href="<?php the_permalink(); ?>"><?php the_title('', ''); ?></a></h2></span>

Would appreciate an answer using my code but any help is most welcomed

5

I’m not sure what you mean by ‘first’ taxonomy… but,

$terms = get_the_terms( $post->ID, 'mytaxonomy' );

returns an array of taxonomy term objects, so

$term = array_pop($terms);

Would give you the first term in the array. And then:

echo '<a href="'.get_term_link($term->slug, 'mytaxonomy').'">'.$term->name.'</a>,'

(You may want to include some if statements, in case an empty array or error is returned (see is_wp_error)

Leave a Reply

Your email address will not be published. Required fields are marked *