You can achieve this very easily, just count the array with the terms.
Also if I could suggest something more, I’d check for WP_Error a bit earlier, outside the foreach loop, instead of checking every single term, especially that you’re not using the $term_link variable anywhere.
Just checking if get_the_terms()
returns an array should be enough in this case if you don’t wish to print out the error message.
function customFruitBreadcrumbs() {
$terms = get_the_terms( $post->ID , 'fruit_category' );
// do nothing if you got an error or no terms at all
if ( ! is_array( $terms ) ) {
return;
}
// here's your link for multiple terms
if ( count($terms) > 1 ) {
echo '<a href="https://wordpress.stackexchange.com/fruits/">Fruits</a> » ';
return;
}
// if you got so far you know you've got exactly one term in the array at this point,
// so you can go with the switch and get rid of the foreach loop completely
switch ($terms[0]->name) {
case "Spring":
echo '<a href="https://wordpress.stackexchange.com/fruits/spring-fruits/">Spring Fruits</a> » ';
break;
case "Summer":
echo '<a href="http://wordpress.stackexchange.com/fruits/summer-fruits/">Summer Fruits</a> » ';
break;
case "Autumn":
echo '<a href="http://wordpress.stackexchange.com/fruits/autumn-fruits/">Autumn Fruits</a> » ';
break;
case "Winter":
echo '<a href="http://wordpress.stackexchange.com/fruits/winter-fruits/">Winter Fruits</a> » ';
break;
default:
echo '';
}
}