简体   繁体   中英

Repeater inside Group ACF Wordpress

I'm using Advanced Custom Fields for Wordpress and trying to loop a repeater inside a group. All I get is "Notice: Array to string conversion in..."

What's wrong & how do I fix it?

<?php if( have_rows('start_horlurar') ): while ( have_rows('start_horlurar') ) : the_row();  ?>

<?php $horlur = get_sub_field('horlur'); ?>

<?php if( have_rows( $horlur['arsmodeller_lankar']) ): while ( have_rows($horlur['arsmodeller_lankar']) ) : the_row();  ?>

<?php echo get_sub_field('lank'); ?>

<?php endwhile; endif; ?>

<?php endwhile; endif; ?>

I believe this was answered correctly, but it did not seem clear enough for those looking for a generic implementation.

<?php

if( have_rows('your_group') ): while ( have_rows('your_group') ) : the_row(); 

    if( have_rows('your_repeater') ): while ( have_rows('your_repeater') ) : the_row();       

        echo get_sub_field('repeater_sub_field');

    endwhile; endif;

endwhile; endif;

?>

Normally with groups you can reach specific subfields by using:

<?php 

$group_var = get_field['your_group']; 

$group_sub_field_var = $group_var['group_sub_field']

?>

However, it seems with repeaters nested inside of groups you cannot use this strategy and are forced to loop through a group first using have_rows() to even reach the repeater.

If you look at the group documentation on ACF it mentions how looping through a group is done like a repeater. Also thehave_rows() documentation has more detail about nested loops using have_rows() .

In nested ACF Repeaters, you need not to add the reference of parent repeater - just add the repeater name alone. Try like this.

<?php
if( have_rows('start_horlurar') ): while ( have_rows('start_horlurar') ) : the_row(); 
    echo get_sub_field('horlur');
    if( have_rows('arsmodeller_lankar') ): while ( have_rows('arsmodeller_lankar') ) : the_row(); 
        echo get_sub_field('lank');
    endwhile; endif;
endwhile; endif;
?>

UPDATED CODE: You need to loop the ACF Group field too like ACF Repeater. Try like this.

<?php
if( have_rows('start_horlurar') ): while ( have_rows('start_horlurar') ) : the_row(); 
    if( have_rows('horlur') ): while ( have_rows('horlur') ) : the_row();       
        if( have_rows('arsmodeller_lankar') ): while ( have_rows('arsmodeller_lankar') ) : the_row(); 
            echo get_sub_field('lank');
        endwhile; endif;
    endwhile; endif;
endwhile; endif;
?>

I find the double loop messy and not needed. I realize this is old, but I just had this issue and didn't want to have two loops.

For my group ('group') and my repeater ('repeater'), with a subfield of ('subfield') this is what I did.

     $group = get_field('group');
     $repeaters = $group['repeaters'];
     foreach($repeaters as $repeater) {
         echo $repeater["subfield"];
       }

Super easy, and it's much more clean. You can add your 'if' statements if needed and not controlled my mandatory fields.

I find this method important for quick and dirty. I use groups for almost everything to be able to create a better user experience for the custom fields in the back end. Most of my custom fields are in groups and grabbing the args, I want it to be as little code and as clean as possible.

Let me know if you guys find any issues with this method, especially in regards to performance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM