Categories
Development

Drupal 7 Only Display 1 Image in Teaser

ok, I was working with a content type with an image field that could hold multiple images (0-4 say). Using the Teaser view mode it would display all the images that were associated with that node, which was really cool in the Default view mode, but not so much the Teaser view mode. I only want it to display 1 image there.

So of course, there’s multiple ways to do this, but I found this approach to be the best for me.

I decided to create and edit an template file in this case since there was enough overall customization going on with this node to warrant such changes. So I created a template file name node–myContentType.tpl.php  based on node.tpl.php and stored in in my theme templates directory.

Easy enough. I then threw in there some crazy text just to make sure I was working with the right template file.

Then I just made the following edits to node–myContentType.tpl.php

if ($view_mode == 'teaser') {
  print render($content['myDemoImage'][0]);
} else {
  print render($content['myDemoImage']);
}

The if statement checks what view mode you are in, if it’s teaser which is the one I want to only display 1 image in then it just renders the first photo in myDemoImage field, else display them all.

That’s it, not too bad. Another reason why I do enjoy working with Drupal.

Leave a Reply

Your email address will not be published. Required fields are marked *