Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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!

<?php  
        $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  

        ?>  
share|improve this question

1 Answer 1

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>' ); 
}
share|improve this answer
    
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

 
discard

By posting your answer, you agree to the privacy policy and terms of service.