Hi there, I’m using the repeater field with a shortcode function: (https://www.advancedcustomfields.com/resources/repeater/)
function myrepeater() {
global $post;
$my_id = get_the_ID($post);
if( have_rows('repeater_field_name', $my_id) ):
while ( have_rows('repeater_field_name', $my_id) ) : the_row();
the_sub_field('sub_field_name', $my_id);
endwhile;
else :
endif;
}
add_shortcode('repeatme', 'myrepeater');
I created a page (classic editor) with text, headings and my shortcode:
This is classic text typed into the classic text editor.
[repeatme]
BUT the output of repeatme isn’t rendered at that specific position but before my entered text. Any idea how to solve this issue?
Shortcodes need to return the content to display, the reason it appears before the content is that you’re echoing out content using the_sub_field before the content is displayed.
function myrepeater() {
ob_start()
global $post;
$my_id = get_the_ID($post);
if( have_rows('repeater_field_name', $my_id) ):
while ( have_rows('repeater_field_name', $my_id) ) : the_row();
the_sub_field('sub_field_name', $my_id);
endwhile;
else :
endif;
return ob_get_clean();
}
add_shortcode('repeatme', 'myrepeater');
Awesome – thank you, a lot of headache just vanished.