Show only one image

3 posts by 2 authors in: Forums > CMS Builder
Last Post: July 2, 2015   (RSS)

By Dave - July 1, 2015

Hi dccreatives, 

There's a couple ways to show the first image.

One is to just "break" out of the foreach loop after the first iteration:

<?php foreach ($record['main_image'] as $upload): ?>
  <?php if ($upload['isImage']): ?>
  <a href="<?php echo $record['link'] ?>"><img src="<?php echo $upload['urlPath'] ?>" alt="" class="img-bord" width="150" height="150" border="0" /></a> <?php endif ?>
  <?php break; ?>
<?php endforeach ?>

Another would be to not use a foreach loop and refer to the first image directly (untested code): 

<?php $upload = @$record['main_image'][0]; // php array's start counting at zero

<?php if (@$upload['isImage']): ?>
<a href="<?php echo $record['link'] ?>"><img src="<?php echo $upload['urlPath'] ?>" alt="" class="img-bord" width="150" height="150" border="0" /></a>
<?php else: ?>
  No image!
<?php endif ?>

Let me know if one of those options works for you.  Cheers!

Dave Edis - Senior Developer

interactivetools.com

By dccreatives - July 2, 2015

Thanks. I used the first coding and it worked.