Repeatable fields

Any field type and even blocks of fields (sections) can be defined as "repeatable".

This means that rather than having a 1:1 relation between a field and its value, a field's value can become an array of values. The actual number of occurrences doesn't need to be known when you define the field, rather the number of values is defined dynamically by you when editing the field for a specific object.

The ACF editor will show you each field's or section's value index ("1 of 2", "3 of 10" etc.) next to each field or section to help you keep track of the defined values.

Use the controls (shown above) to move fields or sections up or down in sequence or to either remove the occurrence or to 'repeat' it. Repeating a field or a section will insert another occurrence after the position of the element being repeated.

But what is this for? Let's say you need to define a list of "technical specifications" for products as value pairs, e.g.:

Weight 16lbs
Material Nylon
Waterproof Yes

but don't know in advance how many of these you need - there could be 3 of these for one product but 20 of them for another.

So you define a "spec_title" field and a "spec_value" field in a section of their own and set this section as "repeatable".

Then, when you edit your custom fields for a specific product, you can adjust the number of specifications needed as you see fit. Of course there are many other use cases like Q&A sections etc., but hopefully this gives you an idea - just be creative.

How to loop over a repeatable field

Arrays stored in Shopify metafield are automatically converted to an iterable array when used from Liquid. This makes it easy to loop over the values as well as access individual items via their index as shown here for the example:

<p>Technical specifications:</p>  
<ul>
{% for value in product.metafields.accentuate.spec_title %} 
  <li>{{ value }}: {{ product.metafields.accentuate.spec_value[forloop.index0] }}</li>
{% endfor %} 
</ul>

Be sure to loop over a field, that you know will have content. If all occurrences of a repeatable field are empty, the metafield will not exist and your loop will not execute.

Sections are not custom fields in their own right, so you cannot reference them via Liquid e.g. to loop over the contained fields

You can use Liquid's array filters like first, last, size, sort, sort_natural, uniq etc. to manipulate the arrays stored in the metafields and thereby control how the values are presented via the store front.

Please note that a repeatable field stored as an array can still contain multiple selection of values in each of the array's elements either separated with "pipe" symbols ("|") or as individual arrays in their own right.

You can define an existing non-repeatable field with values already in place to be repeatable, but the underlying metafields won't become an array before any value(s) are saved via the ACF editor. Similar, if you remove the repeatable setting from a field where multiple values have already been saved, the underlying metafield won't revert from an array to a string before a new value is saved via the ACF editor - the editor will suggest the value from the first occurrence as the default.

When defining a section as being repeatable, all fields belonging to that section are marked repeatable as well. This is what you'd expect, but removing an already set "repeatable" checkmark for a section does not remove the "repeatable" setting for the individual fields as you might expect. They will revert to being repeatable on a single-field basis and you'll have to edit them individually to match your needs. This "quirk" has to do with ACF being backwards compatible with individual fields defined as repeatable before it was possible to do it for sections as well. 

Did this answer your question? Thanks for the feedback There was a problem submitting your feedback. Please try again later.

Still need help? Contact Us Contact Us