1
votes

I've created a simple gallery in Drupal using a custom content type. It's really just an article with an image field that can have an unlimited number of items. I've created a custom template for this content type (node--gallery.tpl.php) and for the full page I'm satisfied with the look and feel.

The one thing I'm stuck on is displaying only the first few images in the teaser. I want to display just the first row of images in the teaser (that first 3 images). In the template file, $content['field_images'] contains all the images for the gallery but print render($content['field_images'] outputs all the images (as I would expect).

So, how do I get Drupal to just output the first three items from $content['field_images']? An improvement I would be interested in is how would I get Drupal to output 3 random images although if I know how to cut down the number of images rendered I'm confident I can work that one out myself.

2
Well, what is that variables content? Is it a concatenated html string with <img> tags? Or is it an array?mario
@mario I'm guessing you don't know Drupal if you're asking. I've updated the question with the full output of $content['field_images'], it's rather long...Endophage
Well why, no I don't. It's easy to take a guess on the limited information, but a variable dump would clear up your description.mario
@mario The var_dump of the variable is too large to post... SO won't actually let me publish the post with it.Endophage
@mario Drupal uses pretty complex arrays to pass information around and the render function knows how to process the arrays correctly. However, you have to pass the right part of the array to render() for it to work and I've tried a few things and can't seem to get it right.Endophage

2 Answers

1
votes

In your template, find the appropriate array structure that has the image file names in it. If you can't find it in $content, look at the var_dump output of $node. Write a for loop to iterate over the first three items and for each of these items call either theme_image() or if you have image styles you want to use, theme_image_style(). Pass in the appropriate configuration array to either using the file name you retrieved from the array.

2
votes

Instead of doing a var_dump or iterating over the array why not install the devel module and make use of its dpm() function which gives a nice output.