Skip Navigation

[Resolved] Possible to count number of repeating fields with 'echo types_render_field'?

The Toolset Community Forum is closed, for technical support questions, please head on to our Toolset Professional Support (for paid clients), with any pre-sale or admin question please contact us here.
This support ticket is created 7 years, 8 months ago. There's a good chance that you are reading advice that it now obsolete.
This is the community support forum for Types plugin, which is part of Toolset. Toolset is a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients and people who registered for Types community support can post in it.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 3 replies, has 2 voices.

Last updated by daveM 7 years, 8 months ago.

Assisted by: Luo Yang.

Author
Posts
#430960

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?

#431152

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

#431356

Hi Luo,

Thanks for the reply! Great solution!

I've tried your code and it's working perfectly as expected.

Thanks again!

#557838

Just resolving this thread on my end.

The forum ‘Types Community Support’ is closed to new topics and replies.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.