I'm using this code to output items from one of my repeating fields:
<li class="item img" data-owl-show="1">
<?php
$image_count = get_post_meta(get_the_ID(), 'gallery-images');
$i = count($image_count);
echo types_render_field('gallery-images', array('separator'=>'</li><li class="item img" data-owl-show="' . $image_count . '">')); ?>
</li>
I would like to use that $image_count variable to echo the index number of each corresponding item in the array. Like this:
<li data-owl-show="1"><img src="the/output/image/path1.jpg" /></li><li data-owl-show="2"><img src="the/output/image/path2.jpg" /></li><li data-owl-show="3"><img src="the/output/image/path3.jpg" /></li>
I found code from this post (https://toolset.com/forums/topic/count-number-of-repeating-fields/) to count the number of fields and echo the total number, but is there a way to display the corresponding index number for each item?
Dear dave,
I suggest you modify the PHP codes as below:
<?php
$images = get_post_meta(get_the_ID(), 'wpcf-gallery-images');
foreach($images as $k=>$image){
echo '<li data-owl-show="' . $k . '"><img src="' . $image . '" /></li>';
}
?>
More help:
hidden link
Hi Luo,
Thanks for the reply! Great solution!
I've tried your code and it's working perfectly as expected.
Thanks again!
Just resolving this thread on my end.