高级自定义字段无法在中继器 have_rows() 内执行嵌套循环中继器不执行任何操作

advanced custom fields can't do nested loops repeater inside repeater have_rows() not doing anything

我在中继器内部有一个中继器,我需要循环播放其中的内容。

我正在关注这个:https://www.advancedcustomfields.com/resources/have_rows/

这是我的字段结构:

agenda_section_events (parent/repeater)
    agenda_event_time_pacific (text)
    agenda_event_time_eastern (text)
    agenda_event_title (text)
    agenda_event_speakers (repeater)
        agenda_event_speaker_headashot (image array)
        agenda_event_speaker_name (text)
        agenda_event_speaker_title (text)
        agenda_event_speaker_content (wysiwyg editor)

因此,据我所知,考虑到这一点,我需要遍历 agenda_section_events 并在其中获取行 https://www.advancedcustomfields.com/resources/have_rows/

因此代码如下所示:

<div class="tc__agenda-items">
  <?php foreach($agendaSectionEvents as $agendaSectionEvent): ?>
    <div class="tc__agenda-item">
      <div class="tc__agenda-time">
        <div class="tc__agenda-time--pacific">
          <?php echo $agendaSectionEvent['agenda_event_time_pacific'] ?>
        </div>
        <div class="tc__agenda-time--eastern">
          <?php echo $agendaSectionEvent['agenda_event_time_eastern'] ?>
        </div>
      </div>
      <h3><?php echo $agendaSectionEvent['agenda_event_title'] ?></h3>
      <div class="tc__agenda-speaker-list">
        <!-- DEBUG LINE - BUT ITS EMPTY -->
        <div style="color: red;"><?php echo have_rows('agenda_event_speakers') ?></div>
        <?php if(have_rows('agenda_event_speakers')): ?>
          <div class="tc__agenda-speaker">
            <?php while(have_rows('agenda_event_speakers')): ?>
              <div class="tc__agenda-speaker-headshot">
                <img src="<?php the_sub_field('agenda_event_speaker_headashot')['url'] ?>" alt="<?php the_sub_field('agenda_event_speaker_headashot')['alt'] ?>">
              </div>
              <div class="tc__agenda-speaker-info">
                <h4><?php the_sub_field('agenda_event_speaker_name') ?></h4>
                <p><?php the_sub_field('agenda_event_speaker_title') ?></p>
              </div>
            <?php endwhile ?>
          </div>
        <?php endif ?>
      </div>
      <a href="#">Learn More</a>
    </div>
  <?php endforeach ?>
</div>

我真的很困惑,因为我的所有字段名称都是正确的,第二个循环中的字段是子字段,但没有显示任何内容。

为什么会这样?

然后我想这是因为我在 a 中做了一个 for each 行,所以我尝试像 acf 文档一样在一段时间内做一段时间

  <div class="tc__agenda-items">
    <?php while(have_rows('agenda_section_events')): ?>
      <div class="tc__agenda-item">
        <div class="tc__agenda-time">
          <div class="tc__agenda-time--pacific">
            <?php echo $agendaSectionEvent['agenda_event_time_pacific'] ?>
          </div>
          <div class="tc__agenda-time--eastern">
            <?php echo $agendaSectionEvent['agenda_event_time_eastern'] ?>
          </div>
        </div>
        <h3><?php echo $agendaSectionEvent['agenda_event_title'] ?></h3>
        <div class="tc__agenda-speaker-list">
          <!-- DEBUG LINE - BUT ITS EMPTY -->
          <div style="color: red;"><?php echo have_rows('agenda_event_speakers') ?></div>
          <?php if(have_rows('agenda_event_speakers')): ?>
            <div class="tc__agenda-speaker">
              <?php while(have_rows('agenda_event_speakers')): ?>
                <div class="tc__agenda-speaker-headshot">
                  <img src="<?php the_sub_field('agenda_event_speaker_headashot')['url'] ?>" alt="<?php the_sub_field('agenda_event_speaker_headashot')['alt'] ?>">
                </div>
                <div class="tc__agenda-speaker-info">
                  <h4><?php the_sub_field('agenda_event_speaker_name') ?></h4>
                  <p><?php the_sub_field('agenda_event_speaker_title') ?></p>
                </div>
              <?php endwhile ?>
            </div>
          <?php endif ?>
        </div>
        <a href="#">Learn More</a>
      </div>
    <?php endwhile ?>
  </div>
<?php endif ?>

自从实施此更改后,现在页面只是……无法加载!? 什么!?

如何在高级自定义字段中进行嵌套 for 循环?

您需要添加the_row();在你的 while 循环中。无论嵌套与否,所有 ACF 循环的格式都相同。我为保持简单而做的事情是将 if/while/the_row 内容全部放在一行中,见下文:

像这样开始循环:

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

这样结束循环:

<?php endwhile; endif; ?>

您可以随心所欲地设置要嵌套的循环数,只需简单地复制父循环行并创建一个嵌套循环,然后当然将您的行更改为嵌套循环。

我在下面调整了你的代码,试一试。

您还应该在使用它的循环中定义您的子字段数组值,只是让事情更清晰 – 或者只是使用 the_sub_field 来回显您的子字段值,因为它是一个短函数。

<div class="tc__agenda-items">
<?php if (have_rows('agenda_section_events')): ?>
 <?php while (have_rows('agenda_section_events')) : the_row(); ?>
    <div class="tc__agenda-item">
        <div class="tc__agenda-time">
            <div class="tc__agenda-time--pacific">
                <?php echo $agendaSectionEvent['agenda_event_time_pacific'] ?>
            </div>
            <div class="tc__agenda-time--eastern">
                <?php echo $agendaSectionEvent['agenda_event_time_eastern'] ?>
            </div>
        </div>
        <h3><?php echo $agendaSectionEvent['agenda_event_title'] ?></h3>
        <div class="tc__agenda-speaker-list">
            <!-- DEBUG LINE - BUT ITS EMPTY -->
            <div style="color: red;"><?php // echo have_rows('agenda_event_speakers') ?></div>
            <?php if (have_rows('agenda_event_speakers')) ?>
            <div class="tc__agenda-speaker">
                <?php while (have_rows('agenda_event_speakers')) : the_row(); ?>
                <div class="tc__agenda-speaker-headshot">
                    <img src="<?php the_sub_field('agenda_event_speaker_headashot')['url'] ?>" alt="<?php the_sub_field('agenda_event_speaker_headashot')['alt'] ?>">
                </div>
                <div class="tc__agenda-speaker-info">
                    <h4><?php the_sub_field('agenda_event_speaker_name') ?></h4>
                    <p><?php the_sub_field('agenda_event_speaker_title') ?></p>
                </div>
                <?php endwhile ?>
            </div>
            <?php endif ?>
        </div>
        <a href="#">Learn More</a>
    </div>
    <?php endwhile ?>
<?php endif ?>