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

yched - August 12, 2007 - 12:40
Status:active» fixed

<?php
<div class="imageBlock">
<?
php foreach ((array)$field_image as $delta => $item) {
  if (
$delta > 0) {
?>

<?php
print $item['view'];
?>

<?php
}
  }
?>


?>

#2

yched - August 12, 2007 - 12:46

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

ericprk - August 12, 2007 - 16:03

Worked great! Thanks for the help.

#4

Anonymous - August 26, 2007 - 16:04
Status:fixed» closed

#5

bobdalob - January 8, 2008 - 16:57
Status:closed» 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.

#6

Rob T - January 27, 2008 - 22:54

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

#7

Rick Hood - January 28, 2008 - 16:41

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

Rick Hood - January 28, 2008 - 16:50

Sorry spoke too soon...

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

#9

Rick Hood - January 28, 2008 - 16:55

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

dopry - May 12, 2008 - 20:00
Status:active» closed

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

#11

Jeff Burnz - May 12, 2008 - 20:09

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

 
 

Drupal is a registered trademark of Dries Buytaert.