Last active
August 22, 2023 13:19
-
-
Save SilverFoxA/50498e70f32e397c8efa4bcfe0194c2a to your computer and use it in GitHub Desktop.
ACF Repeater For Elementor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This is a gist of this ACF Repeater for Elementor plugin - https://wordpress.org/plugins/acf-repeater-for-elementor/ | |
# Current version - 1.5 | |
In a widget or parent `section` add a class `repeater_{name_of_the_repeater}`. Replace {name_of_the_repeater} with the actual name of the repeater field. | |
Inside the above widget, add a text field, and add the name of the sub fields of your repeater. In my case I had to showcase, the days a location is open. | |
I structured my repeater as | |
- Opening Hours (opening_hours) //This is the repeater field | |
-- Time Text (time_text) //This handles text, I haven't checked with any other fields yet | |
Which means `time_text` is a field of the repeater field. Thus, add `#time_text` in your text block. When the page renders, it will automatically replace with the actual value | |
# Current version of the plugin 1.5 won't work with elementor container. Make the following changes in your `function.php` for it to work with containers. | |
# Current version listens to `sections` hence, add additional actions to listen to elementor contianers | |
``` | |
function arfe_prepare_content_by_repeater($content, $repeater_name) | |
{ | |
$repeater = get_field($repeater_name); | |
if (!$repeater || count($repeater) == 0) { | |
return ""; | |
} | |
$new_view = ''; | |
foreach ($repeater as $row) { | |
$single_content = $content; | |
foreach ($row as $key => $value) { | |
$single_content = str_replace("#" . $key, $value, $single_content); | |
} | |
$new_view = $new_view . '' . $single_content; | |
} | |
return $new_view; | |
} | |
function arfe_check_if_repeater_class_in_widget($widget, $classes_key = '_css_classes') | |
{ | |
$classes = $widget->get_settings()[$classes_key]; | |
$classes = explode("repeater_", $classes); | |
if (count($classes) > 1) { | |
$repeater_name = explode(" ", $classes[1])[0]; | |
return $repeater_name; | |
} | |
return false; | |
} | |
add_action('elementor/frontend/container/before_render', function ($section) { | |
// Catch output | |
ob_start(); | |
}); | |
// And then | |
add_action('elementor/frontend/container/after_render', function ($section) { | |
// Collect output | |
$content = ob_get_clean(); | |
// Alter the output anyway you want, in your case wrapping | |
// it with a classed div should be something like this | |
// make sure to echo it | |
if ($repeater_name = arfe_check_if_repeater_class_in_widget($section, 'css_classes')) { | |
print_r($repeater_name); | |
echo arfe_prepare_content_by_repeater($content, $repeater_name); | |
} else { | |
echo $content; | |
} | |
}); | |
``` |
Author
SilverFoxA
commented
Mar 13, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment