Hello Everyone, I have a repeater field called services, which has a sub field called service name, which is a post object.
I use the while loop to echo out the service name separated by commas, but I have a trailing comma at the end that looks awful.
I have looked around for an answer with no luck. Does anyone have an idea.
here is the code:
if( have_rows('services') ):
while ( have_rows('services') ) : the_row();
$service_name = get_sub_field('service_name');
echo $service_name . ', ';
endwhile; endif;
Hi @juanmtorrijos
You can use the get_row_index() to get the current row index and then only show the comma if it’s not the first row like this:
if( have_rows('services') ):
while ( have_rows('services') ) : the_row();
$service_name = get_sub_field('service_name');
if( get_row_index() != 1 ){ echo ', ' }
echo $service_name;
endwhile;
endif;
I hope this helps 🙂
Beautiful! Thank you again James!