Advanced Custom Fields (ACF) Repeater Shortcode Loop

How to create an Advanced Custom Field (ACF) repeater shortcode that loops through to include each repeater subfield. Credit goes to FransicoG:

Include the following code in your child theme’s function.php file:

* ACF Pro repeater field shortcode
* @attr {string} field - (Required) the name of the field that contains a repeater sub group
* @attr {string} sub_fields - (Required) a comma separated list of sub field names that are part of the field repeater group
* @attr {string} post_id - (Optional) Specific post ID where your value was entered. Defaults to current post ID (not required). This can also be options / taxonomies / users / etc
function my_acf_repeater($atts, $content='') {
"field" => null,
"sub_fields" => null,
"post_id" => null
), $atts));

if (empty($field) || empty($sub_fields)) {
// silently fail? is that the best option? idk
return "";

$sub_fields = explode(",", $sub_fields);

$_finalContent = '';

if( have_rows($field, $post_id) ):
while ( have_rows($field, $post_id) ) : the_row();

$_tmp = $content;
foreach ($sub_fields as $sub) {
$subValue = get_sub_field(trim($sub));
$_tmp = str_replace("%$sub%", $subValue, $_tmp);
$_finalContent .= do_shortcode( $_tmp );

else :
$_finalContent = "$field does not have any rows";

return $_finalContent;

add_shortcode("acf_repeater", "my_acf_repeater");

Shortcode for Advanced Custom Fields Repeater rows
Setup a repeater field, in this example we’ll call it “example-row”

and you give it the following sub fields:

example-name, text
example-phone, text
example-image, image, returns URL
in your post content setup the shortcode like this:

[acf_repeater field="example-row" sub_fields="example-name, example-phone, example-image"] User: %example-name%
Phone: %example-phone%
profile pic: %example-image%

notice how the list of comma separated items within the sub_field attribute become %variables% accessible within the repeater shortcode. This allows you to create a template row for your repeater. These items get trimmed so spaces around the commas in the list are ok.