Support

Account

Home Forums Front-end Issues ACF Data not visible from loop

Solving

ACF Data not visible from loop

  • Hi,

    I have a Flexible content group with 2 columns. 1 for title and one for anchor id.
    See pic 1.

    I’m trying to loop out the anchor links(id) and the title like this:

    <?php
    $links = get_sub_field('links');
    ?>
    
     <?php foreach ($links as $link) :
          
            $anchor = ($link['id']) ? 'href="#' . $link['id'] . '"' : "";
          ?>
            <li class="side-menu__item"> 
              
            <a class="menu__link" <?= $anchor ?>>
                <?= $link['title']; ?>
              </a>
            </li>
          <?php endforeach ?>

    I’m doing this on the frontpage, however i get no data. It shows nothing.

    when I do <?php var_dump($links); ?> i get bool(false)

  • get_sub_field() needs to be used inside of a have_rows() loop.

  • Okay so something like this?

      <?php if( have_rows('links') ): ?>
          <?php 
           $links = get_sub_field('links');
          foreach ($links as $link) :
            $anchor = ($link['id']) ? 'href="#' . $link['id'] . '"' : "";
          ?>
            <li class="side-menu__item"> 
              
            <a class="menu__link" <?= $anchor ?>>
                <?= $link['title']; ?>
              </a>
            </li>
          <?php endforeach ?>
          <?php endif; ?>
  • No, what is the name of your repeater and what is the name of the sub field?

    You can either do

    
    $links = get_field('links');
    

    or

    
    while (have_rows('links')) {
      the_row();
      $link = get_sub_field('your_sub_field_name_here');
    }
    
Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.