Instantly share code, notes, and snippets.

Embed
What would you like to do?
An Advanced Custom Fields shortcode that allows to loop through a field with a repeater. This only handles simple cases, it can't handle nested repeater fields
<?php
/**
* ACF Pro repeater field shortcode
*
* I created this shortcode function because it didn't exist and it was being requested by others
* I originally posted it here: https://support.advancedcustomfields.com/forums/topic/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='') {
extract(shortcode_atts(array(
"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 );
endwhile;
else :
$_finalContent = "$field does not have any rows";
endif;
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%
[/acf_repeater]

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.

Important

You can NOT nest repeater shortcodes as library is right now. If you need to then I would recommend adding more shortcodes with different names but pointing to the same function like this:

add_shortcode("acf_repeater", "my_acf_repeater");
add_shortcode("acf_sub_repeater", "my_acf_repeater");

and now you can nest it.

[acf_repeater field="example-row" sub_fields="example-name,  example-phone"]
  <div class="user">User: %example-name%</div><div class="phone">Phone: %example-phone%</div>
  [acf_sub_repeater field="example-sub-row" sub_fields="sub-item1, sub-item2"]
    %sub-item1% %sub-item2%
  [/acf_sub_repeater]
   You can put nested repeaters adjacent to each other, you just can't nest it again. For that you'll need to make more shortcodes
  [acf_sub_repeater field="example-sister-row" sub_fields="sub-item1, sub-item2"]
    %sub-item1% %sub-item2%
  [/acf_sub_repeater]
[/acf_repeater]

TODO: Figure out a better way to handle nesting rows without having to create more copies of the shortcode.

@dkeys222

This comment has been minimized.

dkeys222 commented May 4, 2018

How can i separate each repeater field value with a line?

@kubik101

This comment has been minimized.

kubik101 commented May 7, 2018

FYI - I needed to remove the space after the comma for the comma separated sub_fields so that I had the following:
[acf_repeater field="example-row" sub_fields="example-name,example-phone,example-image"]

@dkeys222

This comment has been minimized.

dkeys222 commented May 7, 2018

how can I display only certain rows of the custom fields?

@meugamer

This comment has been minimized.

meugamer commented May 12, 2018

Say my friend. In its first version I was able to run without problems. Now I have a hard time making it work in this new version.

@notechup

This comment has been minimized.

notechup commented May 31, 2018

Hi! Thanks for sharing this. Could be just what I need to show some repeater fields on my page with Elementor, as it does not seemlike they support those fields for dynamic content yet.

I get a fatal error though when activating the plugin. because I am missing the post_id, I just left it at null (since it was optional), but that doesn't work :)

Let's say I want to show this field on the author archive, so it needs to load the field from the same user profile that is being viewed. Would I need to change the post_id all over? Any help will be appreciated :) Thanks

@ryanjmcdermott

This comment has been minimized.

ryanjmcdermott commented Jun 8, 2018

I can't seem to get this to work properly. :/ here's what my situation looks like.

I've got a repeater field called "hours" and a sub-field called "open_hours".

My shortcode looks like this:

[acf_repeater field="hours" sub_fields="open_hours"] %open_hours% [/acf_repeater]

If I could get some help on this, that'd be great! It's the exact solution I've been looking for. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment