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 post type Holiday associated with a taxonomy Country. On my single-holiday.php, I use the taxonomy to display in the title. For example Holiday in Spain (Spain being the tax)

Rarely, but it can happen, a holiday can be in 2 taxonomy terms. Creating a problem for my title display as I use get_the_terms.

Somebody has better solution for me.

My code is as follow

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


            if($terms) {

               foreach( $terms as $termcountry ) {

            ?>
            <h1>Holiday in <?php echo $termcountry->name;?></h1>}}
share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

you can change you code a bit :

$taxonomy = 'country';
$terms=get_the_terms($post->ID,$taxonomy);
if($terms) {
    echo '<h1>Holiday ';
    $total_count = count($terms);
    $country_count = 1;
    foreach( $terms as $termcountry ) {
        if ($country_count = 1){
            echo 'in '.$termcountry->name;
        }else{
            if ($total_count = 2){
                echo ' And '.$termcountry->name;
            }else{
                echo ', '.$termcountry->name;
            }
        }
        $country_count = $country_count + 1 ;
    }
    echo '</h1>';
}

This will output in case of one term:

<h1>Holiday in Spain</h1>

in case of two terms:

<h1>Holiday in Spain And Japan</h1>

and in case of more then two terms:

<h1>Holiday in Spain, Japan, England</h1>

hope this helps

share|improve this answer
    
Top notch answer -- thanks very much - works like a charm –  salocin Mar 14 '11 at 16:55
    
Glad i could help :) –  Bainternet Mar 14 '11 at 18:53
add comment

Your Answer

 
discard

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

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