I'm building a site with Drupal 7.2, and using several useful modules (Views, Display Suite, and 20+ more).
I've added an image field in a content type (article), and set the 'Field Settings' -> 'number of values' to 5, which means users can upload 5 images for this field.
Under 'Full Content' view mode, I'd like to display all the images, but How DO I display only ONE image under 'Teaser' mode? Is there any modules can do this?
I solved this problem by adding a new template for this field type like field--field_image.tpl.php in my case with the following code:
// Reduce image array to single image in teaser view mode
if ($element['#view_mode'] == 'teaser') {
$items = array(reset($items));
}
print render($items);
Hope this helps.
Edit: Here's the (probably) correct way to do it:
function MYTHEME_process_field(&$vars) {
$element = $vars['element'];
// Field type image
if ($element['#field_type'] == 'image') {
// Reduce number of images in teaser view mode to single image
if ($element['#view_mode'] == 'teaser') {
$item = reset($vars['items']);
$vars['items'] = array($item);
}
}
}
It did not work for me, i had to adjust the code sligthly:
function MYTHEME_process_field(&$vars) {
$element = $vars['element'];
// Reduce number of images in teaser view mode to single image
if ($element['#view_mode'] == 'node_teaser' && $element['#field_type'] == 'image') {
$vars['items'] = array($vars['items'][0]);
}
}
One option without having to change code is to use the module Field Multiple Limit. With this module you can choose how many items to show in which view for fields that allow for multiple entries.
Then on the teaser view of the field you can choose how many items to show or to skip.
Just saw this is already answered in Drupal Stack Exchange
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With