Support

Account

Home Forums Add-ons Repeater Field Remove Trailing Comma in Repeater Field Output

Solved

Remove Trailing Comma in Repeater Field Output

  • 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!

Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Remove Trailing Comma in Repeater Field Output’ is closed to new replies.