I need to have have something like this:

<div class="gal-wrapper">
   <div class="gallery" id="r1"><img src="image01.jpg"></div>
   <div class="gallery" id="r2"><img src="image02.jpg"></div>
   <div class="gallery" id="r3"><img src="image03.jpg"></div>
   <div class="gallery" id="r4"><img src="image04.jpg"></div>....

My CCK Image field name is filed_image. In page.tpl.php I have the following code:

<div class="gal-wrapper">
    <?php foreach ((array)$node->field_image as $item) { ?>
      <div id="" class="gallery"><?php print $item['view'] ?></div>
    <?php } ?>

So with this I am able to print all images form my Image Field.

But I don't know how to dynamically change the value of id="r1", id="r2", id="r3" ....

That value must start from 1 and be auto increment. For example if I have 3 images the id should be id="1", id="2", id="3" or If there are 10 images I need id from 1 to 10.


This code should do it:

<div class="gal-wrapper">
    <?php foreach ((array)$node->field_image as $item_id => $item) { ?>
      <div id="<?php print $item_id+1?>" class="gallery"><?php print $item['view'] ?></div>
    <?php } ?>
  • Thank you! Yes it is working! Problem solved :) – Ivailo Milenkov Oct 20 '11 at 12:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.