Repeater inside Group ACF Wordpress
Asked Answered
A

5

5

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; ?>
Aberration answered 28/8, 2018 at 12:0 Comment(8)
please post minimal codeLowbred
I did, but it disappeared.Aberration
what is this field "lank" text or image or someotherLowbred
It's the sub field in the repeater "arsmodeller_lankar"Aberration
try this, the_field('lank');Lowbred
doesn't make a difference.Aberration
It doesn't seem to loop the repeater at allAberration
Here's a plain and simple guide to repeaters inside groups (wpza.net/use-a-repeater-inside-a-group) if it helps anyone.Diversify
S
14

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;
?>
Starstarboard answered 28/8, 2018 at 12:35 Comment(7)
It dosen't output anything. Not even echo "hello world";Aberration
Can you confirm that start_horlurar is parent repeater name & arsmodeller_lankar is the child repeater under that?Starstarboard
But the lank field seems to be under start_horlurar and not arsmodeller_lankar?Starstarboard
Seems horlur a group field between those, can you just confirm the hierarchy - start_horlurar (repeater) > horlur (group) > arsmodeller_lankar (repeater)?Starstarboard
Yes! :) Therefore the title of the questionAberration
No problem! :) It Worked, you're a genius!Aberration
The updated code it was worked for me (i.e. the group needed to be looped as well). Thanks!Departed
D
24

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().

Deemster answered 24/1, 2019 at 15:19 Comment(0)
R
15

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.

Relate answered 1/12, 2019 at 22:36 Comment(2)
This worked for me, and is neater than the double loopCalysta
...but you're loosing the benefit of checking if group, or repeater, or sub field exists.Toolis
S
14

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;
?>
Starstarboard answered 28/8, 2018 at 12:35 Comment(7)
It dosen't output anything. Not even echo "hello world";Aberration
Can you confirm that start_horlurar is parent repeater name & arsmodeller_lankar is the child repeater under that?Starstarboard
But the lank field seems to be under start_horlurar and not arsmodeller_lankar?Starstarboard
Seems horlur a group field between those, can you just confirm the hierarchy - start_horlurar (repeater) > horlur (group) > arsmodeller_lankar (repeater)?Starstarboard
Yes! :) Therefore the title of the questionAberration
No problem! :) It Worked, you're a genius!Aberration
The updated code it was worked for me (i.e. the group needed to be looped as well). Thanks!Departed
A
0

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; ?>
Astonishment answered 3/5, 2023 at 12:20 Comment(0)
S
0

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.

Succotash answered 1/5, 2024 at 13:40 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Shostakovich

© 2022 - 2025 — McMap. All rights reserved.