3

I'm fairly new in using ACF so I used the code that was displayed in their site to display what is inside of my repeater field.

However, when I try to show the repeater's content it is empty?? This is my code, this is still in trial mode just to see if it's working-which it isn't. My repeater field already has 2 rows but it's not showing up any of those and just displays the else:

// check if the repeater field has rows of data
if( have_rows('map_infogrp') ):

    // loop through the rows of data
    while ( have_rows('map_infogrp') ) : the_row();

        // display a sub field value
        the_sub_field('google_map');
        the_sub_field('branch_name');
        //$street = get_sub_field('street');
        //$district = get_sub_field('district');
        //$phonenum = get_sub_field('phone_number');
        //$email = get_sub_field('email');


    endwhile;
else:

echo 'why is this empty???';

endif;

2 Answers 2

5

You need to specify the page id that you have set the ACF Repeater, otherwise it will get from the current page ID

have_rows($field_name, $post_id); //syntax

So, update your loop inserting the page ID you've entered the repeater data:

if( have_rows('map_infogrp', 'page_id') ):

    // loop through the rows of data
        while ( have_rows('map_infogrp', 'page_id') ) : the_row();

...
2
  • I thought post_id was the id of the fieldgroup, i used the page where it's placed and now it's working. Thank you so much!
    – Amiel M
    Commented Feb 7, 2018 at 3:23
  • I needed to add 'options' as the second parameter as I was using it on the options page. Commented Jun 29, 2018 at 8:02
0

If you have the fields filled in on the specific page it should be showing up. If not, double check the field name(not label) that you used. If you have more than one row make sure you're printing it out as an array too

3
  • the website is a one pager site but the different parts is divided into different pages, will that be in conflict with the code or not?
    – Amiel M
    Commented Feb 7, 2018 at 3:03
  • yes every page has it's own fields you need to fill out. Otherwise you can get the options plugin to have a global storage. Maybe var_dump the subfield and see what comes out? Commented Feb 7, 2018 at 3:07
  • i think the problem is each page was created in wp, not in the backend so it's hard for me to place a php code for it unless it's in shortcode.
    – Amiel M
    Commented Feb 7, 2018 at 3:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.