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.

Comments

yched’s picture

Status: Active » Fixed
<div class="imageBlock">
<?php foreach ((array)$field_image as $delta => $item) { 
  if ($delta > 0) { 
print $item['view'];
 }
  } 


?>

yched’s picture

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>
ericprk’s picture

Worked great! Thanks for the help.

Anonymous’s picture

Status: Fixed » Closed (fixed)
bobdalob’s picture

Status: Closed (fixed) » Active

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.

Rob T’s picture

Thanks for the snippet, yched... it helped a great deal.

rick hood’s picture

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

rick hood’s picture

Sorry spoke too soon...

The above (#7) doesn't work. Back later when I figure it out...

rick hood’s picture

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>" ; 
} 
?>
dopry’s picture

Status: Active » Closed (fixed)

go do theme snippet development and docs in the handbook where people can find it later...

Jeff Burnz’s picture

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...