First image at top of content, remaining images at bottom.
ericprk - August 12, 2007 - 07:40
| Project: | ImageField |
| Version: | 5.x-1.x-dev |
| Component: | Code |
| Category: | support request |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | closed |
Jump to:
Description
I've managed to figure out how to display the first image of a node teaser at the beginning using contemplate:
<?php print $node->field_image[0]['view'] ?>I'd like the remaining images to appear after the teaser text. I can get all the images to display at the end of the text using:
<div class="imageBlock">
<?php foreach ((array)$field_image as $item) { ?>
<div class="image">
<?php print $item['view']; ?>
</div>
<?php } ?>
<br class="clearall" />
</div>My problem is how do I get it to skip field_image[0]. I figure I need to test if something is > 0, but don't know what to use. Any help would be greatly appreciated. My apologies if I'm posting this in the wrong area. I'm fairly new to this.

#1
<?php<div class="imageBlock">
<?php foreach ((array)$field_image as $delta => $item) {
if ($delta > 0) {
?>
<?phpprint $item['view'];
?>
<?php}
}
?>
?>
#2
heh, PHP code filter does not like embedded HTML...
<div class="imageBlock"><?php foreach ((array)$field_image as $delta => $item) {
if ($delta > 0) {?>
<div class="image">
<?php print $item['view']; ?>
</div>
<?php }
} ?>
<br class="clearall" />
</div>
#3
Worked great! Thanks for the help.
#4
#5
This is a useful snippet however when used with content types with optional images, image-free nodes will show the unattractive 'image not found' in place of the 'first' image.
#6
Thanks for the snippet, yched... it helped a great deal.
#7
Regarding #5 above:
I solved this by checking to see if the variable was empty and only print it if its not empty:
<?php$primaryimage = $node -> content['field_image_primary']['#value'];
$primaryimagexists = empty($primaryimage);
if ($primaryimagexists != 0) {
print "<div class='primaryimage'>" . $primaryimage . "</div>" ;
} ?>
The PHP empty function is here:
http://www.php.net/manual/en/function.empty.php
#8
Sorry spoke too soon...
The above (#7) doesn't work. Back later when I figure it out...
#9
OK, this works:
<?php$primaryimage = $node -> content['field_image_primary']['#value'];
$primaryimagexists = $node -> field_image_primary[0]['fid'];
if ($primaryimagexists != 0) {
print "<div class='primary-image'>" . $primaryimage . "</div>" ;
}
?>
#10
go do theme snippet development and docs in the handbook where people can find it later...
#11
old reliable way I have always done this
<?php if (!empty($field_image[0][view])): ?><div><?php print $field_image[0][view]; ?></div>
<?php endif; ?>
edit - sorry, pasted wrong code... my bad, this one uses !empty...