1

I'm sure I'm missing something very straightforward here: I want to prevent the thumbnail slider in my image gallery from being rendered if there's only one image for the node. My first thought was, optimistically:

  <?php if (count($content['field_media']) > 1): ?>
    <div id="carousel" class="flexslider clearfix">
      <div class="slides">
        <?php print render($content['carousel']); ?>
      </div>
    </div>
  <?php endif; ?>

I saw why that didn't work when I examined the node structure. In an example case for a node with one image, I have

 field_media (Array, 1 element)
    und (Array, 1 element)
        0 (Array, 17 elements)  

the last one being the actual file element with an fid, etc. The corresponding field for a node with two images is

 field_media (Array, 1 element)
    und (Array, 2 elements)
        0 (Array, 17 elements)
        1 (Array, 17 elements) 

So the array I need to count is nested inside the 'und' array. I know I've used that in other templates, like $field_external_link['und'][0]['url']. But when I print count($content['field_media']['und']), I get 0. How do I evaluate the number of elements in that array?

1

You can use field_get_items api function to read values.

<?php

$images = field_get_items('node', $node, 'field_media');
if (count($images) > 1) {
  // Print Carousel
}
?>
| improve this answer | |

Your Answer

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

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