Skip to content

Instantly share code, notes, and snippets.

@code5rick
Forked from FranciscoG/acf_repeater_shortcode.php
Last active February 12, 2024 14:43
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save code5rick/a303d5692b2f65dc5e1630bf66ffc251 to your computer and use it in GitHub Desktop.
Save code5rick/a303d5692b2f65dc5e1630bf66ffc251 to your computer and use it in GitHub Desktop.
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
*
* Created by: https://gist.github.com/FranciscoG
* Edit by: Caio Santos
*/
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 if essential attributes are missing
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));
// If a subvalue is an array, then show it
if (isset($subValue[0]->name)) {
$docentes = [];
foreach ($subValue as $docente) {
//echo "<pre>"; // activate this code to see what vars you want to use it, then disable it
//var_dump($docente);
//echo "</pre>";
//$docentes[] = $docente->name; // without link
$docentes[] = '<a href="/' . $docente->taxonomy . '/' . $docente->slug . '" target="_blank">' . $docente->name . '</a>'; //with link - customize your way
}
$subValue = [];
$subValue = $docentes;
}
if (is_array($subValue)) {
$subValue = implode(', ', $subValue);
}
$_tmp = str_replace("%$sub%", $subValue, $_tmp);
}
$_finalContent .= do_shortcode($_tmp);
endwhile;
} 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%
[/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.

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