Advanced Custom Fields (ACF) enables you to attach flexible, repeatable field groups to your WordPress posts and pages. One of the standout features is the repeater field. This tutorial explains how to fetch and display repeater field data using have_rows()
and the_row()
.
Fetching Values from ACF Repeater Fields
Let’s say you have a repeater named “my_repeater_field” containing two subfields: “sub_field_1” and “sub_field_2”. Here’s how you can loop through and display them:
Step 1: Loop Through Repeater Field Rows
Use have_rows()
to check for rows before starting the loop:
<?php if (have_rows('my_repeater_field')) { while (have_rows('my_repeater_field')) { the_row(); $sub_field_1_value = get_sub_field('sub_field_1'); $sub_field_2_value = get_sub_field('sub_field_2'); echo 'Subfield 1: ' . $sub_field_1_value . '<br>'; echo 'Subfield 2: ' . $sub_field_2_value . '<br>'; } } ?>
Step 2: Get Subfield Values
Inside the loop, use get_sub_field()
to pull values from each subfield:
get_sub_field('sub_field_1')
– returns the value of the first subfield.get_sub_field('sub_field_2')
– returns the value of the second subfield.
You can repeat this pattern for more subfields within the repeater group.
Benefits of Repeater Fields
Repeaters are ideal for structured data like team listings, FAQs, or grouped content blocks. With a few lines of code, you can make your site dynamic and manageable.
Further Reading
Now you’re ready to leverage ACF repeater fields effectively in your projects!