Support

Account

Home Forums Add-ons Repeater Field Can't get repeater in group field to return true with have_rows()

Solved

Can't get repeater in group field to return true with have_rows()

  • Hello,

    I have a group field that contains a couple sub fields as well as repeater. I can get the sub fields to display, but I can’t get the repeater to display for the life of me. I’ve worked with tons of repeaters and have never had this issue before.

    Here is a basic version of my code:

    $group = get_field('group_field');
    $heading = $group['heading'];
    $text = $group['text'];
    // repeater field is called 'links_list'
    // repeater sub field called 'link'
    
    <h3><?php echo $heading; ?></h3>
    <?php echo $text; ?>
    <?php if( have_rows('links_list') ) : ?>
    <ul class="links-list">
        <?php while( have_rows('lists_list') ) : the_row(); $link = get_sub_field('link'); ?>
        <li><a href="<?php echo $link['url']; ?>" target="<?php echo $link['target']; ?>"><?php echo $link['title']; ?></a></li>
        <?php endwhile; ?>
    </ul>
    <?php endif; ?>

    The heading and text display but not the list. It’s not passing the if( have_rows('links_list') : check. I tried removing the if statement and just using the while loop but still doesn’t print anything.

    I even tried doing a var_dump on the repeater and it’s showing me the array with all my selected links. So it’s saving the data fine.

    $repeater = get_sub_field('links_list');
    var_dump($repeater);

    I checked every character in my code to make sure I didn’t have an incorrect colon, semi-colon, misspelled field names, etc. Everything looks correct.

    So I tried going to my field group, I dragged the repeater out of the group field and placed it after the group. Went to my page and selected my links again (values were lost when I moved the repeater out of the group field) and now it works perfectly fine. Nothing changed in my code after pulling the repeater out of the group and no longer being nested.

    WP: 5.2.3
    ACF Pro: 5.8.4

    Has anyone else run into this issue?

  • I just noticed I have a typo in my code. The while loop has have_rows('lists_list') where is should have said have_rows('links_list'). Even though I mistyped it here, it was correct in my code. I didn’t copy and past my code because of formatting/indentation reasons. So that is NOT the reason my repeater wasn’t working. I also tried another group with a repeater and did the same thing. Just seems like the have_rows() does not work correctly when inside a group field.

    • RemSEO

    • October 12, 2019 at 1:13 am

    Hi there,

    Could you please try something like this :

    <?php
    $group = get_field('group_field');
    $heading = $group['heading'];
    $text = $group['text'];
    $repeater = $group['links_list']; //
    ?>
    
    <h3><?php echo $heading; ?></h3>
    <?php echo $text; ?>
    <?php if( have_rows($repeater) ) : ?>
    <ul class="links-list">
        <?php while( have_rows($repeater) ) : the_row(); $link = get_sub_field('link'); ?>
        <li><a href="<?php echo $link['url']; ?>" target="<?php echo $link['target']; ?>"><?php echo $link['title']; ?></a></li>
        <?php endwhile; ?>
    </ul>
    <?php endif;?>

    Explanation

    As a repeater field, the group field is a row of datas, but it contains a single row.
    (as it’s explained in the documentation: https://www.advancedcustomfields.com/resources/group/)

    In your code, you tried to have the repeater fields datas, without indicating in which $row your repeater field is (your group field).

    Let me know if it helps.

  • Hi @remseo

    I apologize for the delayed response.

    I tried what you recommended but did not work. However, I did figure out a way to make it work. I had to first surround everything in the have_rows() and the_row() of the group field.

    This is what worked for me:

    $group = get_field('group_field');
    $heading = $group['heading'];
    $text = $group['text'];
    // repeater field is called 'links_list'
    // repeater sub field called 'link'
    if( have_rows('group_field') ) : while( have_rows('group_field') : the_row();
    ?>
        <h3><?php echo $heading; ?></h3>
        <?php echo $text; ?>
        <?php if( have_rows('links_list') ) : ?>
        <ul class="links-list">
            <?php while( have_rows('lists_list') ) : the_row(); $link = get_sub_field('link'); ?>
            <li><a href="<?php echo $link['url']; ?>" target="<?php echo $link['target']; ?>"><?php echo $link['title']; ?></a></li>
            <?php endwhile; ?>
        </ul>
        <?php endif; ?>
    <?php endwhile; endif; ?>
Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.

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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.