2

I would like to display just the first term from a custom post type custom taxonomy.

Inside my loop I've been displaying all the terms using:

<?php echo get_the_term_list( $post->ID, 'traits', 'Physical Traits:', ''); ?>

but I would like to only display the first term.

Thanks!

1 Answer 1

2

A little bit more involved but:

<?php

function get_single_term($post_id, $taxonomy) 
{
    $terms = wp_get_object_terms($post_id, $taxonomy);
    if(!is_wp_error($terms))
    {
        return '<a href="'.get_term_link($terms[0]->slug, $taxonomy).'">'.$terms[0]->name.'</a>';   
    }
}

//example
echo get_single_term(5, 'category');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.