Skip Navigation

[Resolved] Adding "and" before the last item in a repeating field

This support ticket is created 3 years ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

This topic contains 2 replies, has 2 voices.

Last updated by jabienL 3 years ago.

Assisted by: Minesh.

Author
Posts
#2562475

I have a repeating field that I am displaying in a Fields and Text Block. The field usually has 3-5 items in it. I want to know if there is any way to add the word "and" before the last repeated item.

Example:
It currently displays like this:
...IPA, New England IPA, Pale Ale, Wheat Beer, Golden Ale.

but I want it to display like this:
...IPA, New England IPA, Pale Ale, Wheat Beer, and Golden Ale.

Thanks in advance!

#2563109

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

It will require custom shortcode. You can add the custom shortcode to "Custom Code" section offered by Toolset:

add_shortcode('show_repeating_field', 'func_show_repeating_field');
function func_count_rf_entries($atts, $content){
    $atts = shortcode_atts( array(
        'field' => '',
        'post_id' => get_the_ID(),
    ), $atts );
        
    $values = get_post_meta($atts['post_id'], "wpcf-".$atts['field'], false);
$res = '';   
if(!empty($values)){
     $last = array_pop($values);
      $res = join(", ",$values )." and ".$last;
}
    

    return $res;
}

And you can call the shortcode as:

[show_repeating_field   field="field-slug']

Where:
- Replace the "field-slug" with your Types field slug.

You can add the above shortcode to "Custom Code" section offered by Toolset:
=> https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/#adding-custom-php-code-using-toolset

#2563383

My issue is resolved now. Thank you!