I have a custom taxonomy called "Artists" which has the taxonomy singular name set to "Artist".

If a post only has 1 artist I would like the label to be "Artist" instead of "Artists".

How could I achieve this? My current code is below. Thanks!

        $taxo_text = "";  
        $artists = get_the_term_list( $post->ID, 'artists', "<div class='grid_2 details'>
     <ul class='credits'> <li class='smallunderline'>Artists</li><li> ", '</li><li>', '</li></ul></div>' );  

        if ( '' != $artists ) {  
            $taxo_text .= $artists;  

        if ( '' != $taxo_text ) {  

        echo $taxo_text;  

        } // endif  


get_the_terms() returns an array of the terms, and is used by get_the_term_list() to build the links. You can query it and count the number of returned terms. Since the result is cached, it won't query the database again.

$artists = get_the_terms( $post->ID, 'artists' );
if ( $artists ) {
    $artistsLabel = ( 1 == count( $artists ) ? 'Artist' : 'Artists' );
    $taxo_text .= get_the_term_list( $post->ID, 'artists', "<div class='grid_2 details'>
 <ul class='credits'> <li class='smallunderline'>{$artistsLabel}</li><li> ", '</li><li>', '</li></ul></div>' ); 
  • Shouldn't this already be covered if the taxonomy is registered with all the appropriate labels, namingly a singular and plural label? – t31os Dec 3 '10 at 11:53
  • @t31os: You would expect so, but get_the_term_list() is not very advanced and appends just the $before parameter, it makes no attempt to create a sensible default value. – Jan Fabry Dec 3 '10 at 13:40

Your Answer

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