Home › Forums › Add-ons › Repeater Field › while have_rows not working with acf_add_local_field_group defined field.
I had a field group defined containing a repeated set up in WordPress. Within my template I would output it like this:
$repeater_field_name = get_field( 'repeater_field_name', $some_page_id);
if ($repeater_field_name) {
foreach($repeater_field_name as $r){
echo $r['subfield_name'];
}
}
That worked fine.
I then converted the field group to be defined via PHP function acf_add_local_field_group
using the built-in “Export Field Groups” function. Once that was done get_field( 'repeater_field_name', $some_page_id)
would return the row count rather then array. As such I adapted the my code to be a while loop:
if (get_field('repeater_field_name', $some_page_id)) {
echo 'STARTING';
while(have_rows('repeater_field_name', $some_page_id)){
the_row();
echo get_sub_field('subfield_name');
}
}
Something isn’t working here. It will do the first echo, but no while loops are processed.
Any idea what’s going on?
hi @louiswalch
can you paste the code for the add_local_field_group
?
on a side note, if you’re just wanting to have your fields defined via your theme, instead of the database, i would look into the ACF Local JSON: https://www.advancedcustomfields.com/resources/local-json/
It makes things a bit simpler ^. Just remember to sync the fields on your staging/production apps before pulling down your database.
Phil
I’m using the JSON actually to cache my groups. But I thought everytime you make changes locally and push up you have to manually go into the backend and click to refresh the group (circle arrow icon)?
GROUP (truncated example):
if( function_exists('acf_add_local_field_group') ):
acf_add_local_field_group(array(
'key' => 'group_597a31e9a49bd2',
'title' => 'Settings (New)',
'fields' => array(
array(
'key' => 'field_597a32a719ca9',
'label' => 'Co-Chairs',
'name' => 'co-chairs',
'type' => 'repeater',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'collapsed' => 'field_597a32da19caa',
'min' => 0,
'max' => 0,
'layout' => 'table',
'button_label' => 'Add Co-Chair',
'sub_fields' => array(
array(
'key' => 'field_597a32da19caa',
'label' => 'Name',
'name' => 'name',
'type' => 'text',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '',
'maxlength' => '',
),
array(
'key' => 'field_597a32e719cab',
'label' => 'Affiliate',
'name' => 'affiliate',
'type' => 'text',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '',
'maxlength' => '',
),
array(
'key' => 'field_597a32fe19cac',
'label' => 'E-Mail',
'name' => 'e-mail',
'type' => 'email',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '',
),
),
),
),
'location' => array(
array(
array(
'param' => 'page_template',
'operator' => '==',
'value' => 'page-committee-info.php',
),
),
),
'menu_order' => 0,
'position' => 'normal',
'style' => 'default',
'label_placement' => 'top',
'instruction_placement' => 'label',
'hide_on_screen' => '',
'active' => 1,
'description' => '',
));
endif;
TEMPLATE:
<? if ( get_field( 'co-chairs', $settings_page) ) : ?>
<li>
<? var_dump( $settings_page ); ?>
<h2 class="subheader-small">Co Chairs</h2>
<ul class="co-chairs">
<?php while( have_rows('co-chairs', $settings_page) ): the_row(); ?>
<li>
<div class="name"><?=get_sub_field('name'); ?></div>
<div class="Preheader affiliate"><?=get_sub_field('affiliate'); ?></div>
<div class="Preheader email"><a href="mailto:<?=get_sub_field('e-mail'); ?>"><?=the_sub_field('e-mail'); ?></a></div>
</li>
<? endwhile; ?>
</ul>
</li>
<? endif; ?>
@louiswalch you only need to sync them if you’re going to pull down the database and make more edits, else you can just leave it alone. I’ll look this over and let you know if I see anything weird ^
Phil
Hello, wondering if you were able to test this out. It’s nice to know the JSON is an option but I would prefer to define my field group in PHP. Thanks for the help.
hi @louiswalch
I’m afraid i would need a lot more data to help test this, but I would recommend installing a fresh copy of WP locally and copying these fields over one by one to see if you’re still having the issue.
Here is a helpful resource that ACF provides for debugging: https://www.advancedcustomfields.com/resources/debug/
Phil
The topic ‘while have_rows not working with acf_add_local_field_group defined field.’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.