Ok, something not right, I like your code here reduces everything, though what I think is happening is that I can only get the value of $items once it has run through the foreach. This is the problem I was having.
Basically, $items has no value until we run the foreach, or maybe I have things mixed up.
What do you suggest?
Carousel is not added, no matter how many items I have.
As presented, $items is an array that must exist and have at least one element before you start the foreach (otherwise, the foreach would do nothing). What ‘value’ are you attempting to retrieve? An array does not intrinsically have “a” value.
You can nest ternaries, it gets a little uglier, and generally repetition of function tells you to pull the function call out…
<?php $count = count($items); //By definition, $count can only be 0 or greater. ?>
<div class="field-items<?php ($count === 0) ? '' : (($count === 1) ? ' single' : ' carousel'); ?>">
(You cannot add ‘nothing’ (represented as the empty string ‘’) if 0 or 1 and then add single if 1. 1 cannot do both in a single ternary. It’s the same as if…elseif. If the first condition was true, doesnt matter if any subsequent conditions are true, the first set gets done and then jump over the rest.)