oscar
oscar

Reputation: 53

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; ?>

Upvotes: 5

Views: 39505

Answers (5)

This code can works with one or two repeater in one Group

<?php if (have_rows('your_group_name')): ?>
    <?php while (have_rows('your_group_name')): the_row();
        if (have_rows('your_repeater_name')): ?>
            <ul>
            <?php while (have_rows('your_repeater_name')): the_row();
                $your_repeater_item = get_sub_field('your_repeater_item');
            ?>
                <li><?php echo $your_repeater_item; ?></li>
            <?php endwhile; ?>
            </ul>
        <?php endif; ?>
    <?php endwhile; ?>
<?php endif; ?>

If you use two repeaters in Group field, just copy this code twice and replace variables names. I hope this code helps somebody.

Upvotes: 0

danbrown
danbrown

Reputation: 165

I format it like this, I think it's much cleaner:

<?php if(have_rows('features_repeater')): while(have_rows('features_repeater')): the_row(); if(have_rows('features_group')): while(have_rows('features_group')): the_row(); ?>
    
    <h1><?php echo get_sub_field('title'); ?></h1>

<?php endwhile; endif; endwhile; endif; ?>

Upvotes: 0

Jamie Kembel
Jamie Kembel

Reputation: 151

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.

Upvotes: 15

Jared Rice
Jared Rice

Reputation: 445

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 the have_rows() documentation has more detail about nested loops using have_rows().

Upvotes: 24

Outsource WordPress
Outsource WordPress

Reputation: 3799

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;
?>

Upvotes: 14

Related Questions