- 首页
- PHP
- 高级自定义字段无法在转发器内执行嵌套循环转发器 have_rows() 不执行任何操作
高级自定义字段无法在转发器内执行嵌套循环转发器 have_rows() 不执行任何操作
[英] advanced custom fields can't do nested loops repeater inside repeater have_rows() not doing anything
本文介绍了高级自定义字段无法在转发器内执行嵌套循环转发器 have_rows() 不执行任何操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在中继器中有一个中继器,我需要循环其中的内容.
我正在关注这个:https://www.advancedcustomfields.com/resources/have_rows/
这是我的字段结构:
agenda_section_events(父/中继器)议程_事件_时间_太平洋(文本)议程_事件时间_东方(文本)议程_事件_标题(文本)议程_事件_演讲者(中继器)agenda_event_speaker_headashot(图像数组)议程_事件_演讲者_姓名(文本)议程_事件_演讲者_标题(文本)议程_event_speaker_content(所见即所得的编辑器)
考虑到这一点,据我所知,我需要遍历 agenda_section_events
并在其中获取行 https://www.advancedcustomfields.com/resources/have_rows/
所以代码看起来像这样:
<?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 class="tc__agenda-time--eastern"><?php echo $agendaSectionEvent['agenda_event_time_eastern'] ?>
<h3><?php echo $agendaSectionEvent['agenda_event_title'] ?></h3><div class="tc__agenda-speaker-list"><!-- 调试行-但它是空的--><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 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>
<?php endwhile ?>
<?php endif ?>
<a href="#">了解详情</a>
<?php endforeach ?>
我真的很困惑,因为我所有的字段名称都是正确的,并且第二个循环中的字段是子字段但没有显示任何内容.
为什么会这样?
然后我认为这是因为我在 a 中为每个行做了一行,所以我尝试像 acf 文档一样在一段时间内做一段时间
<?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 class="tc__agenda-time--eastern"><?php echo $agendaSectionEvent['agenda_event_time_eastern'] ?>
<h3><?php echo $agendaSectionEvent['agenda_event_title'] ?></h3><div class="tc__agenda-speaker-list"><!-- 调试行-但它是空的--><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 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>
<?php endwhile ?>
<?php endif ?>
<a href="#">了解详情</a>
<?php endwhile ?>
<?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();?>
像这样结束循环:
您可以随意设置要嵌套的循环数,只需简单地复制父循环行并创建一个嵌套的循环行,然后当然将您的行更改为嵌套的行.
我在下面调整了你的代码,试一试.
您还应该在使用它的循环中定义您的子字段数组值,只是让事情更简洁 – 或者只是使用 the_sub_field 来回显您的子字段值,因为它无论如何都是一个简短的函数.
<?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 class="tc__agenda-time--eastern"><?php echo $agendaSectionEvent['agenda_event_time_eastern'] ?>
<h3><?php echo $agendaSectionEvent['agenda_event_title'] ?></h3><div class="tc__agenda-speaker-list"><!-- 调试行-但它是空的--><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 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>
<?php endwhile ?>
<?php endif ?>
<a href="#">了解详情</a>
<?php endwhile ?><?php endif ?>
I have a repeater inside a repeater and I need to loop what is in it.
I'm following this: https://www.advancedcustomfields.com/resources/have_rows/
This is my field structure:
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)
So with that in mind, as I understand it, I need to loop over agenda_section_events
and inside that I need to get rows https://www.advancedcustomfields.com/resources/have_rows/
So that code looks like this:
<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>
I'm really confused because all my field names are right, and the fields inside the second loop are sub fields but nothing is showing.
Why would that be?
I then thought its because I did a have rows inside a for each so then I tried doing a while inside a while like the acf docs
<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 ?>
Since implementing this change, now the page just...doesn't load!? What!?
How do you do a nested for loop in advanced custom fields?
解决方案
You need to add the_row(); inside your while loops. All ACF loops are formatted the same, nested or not. Something I do to keep it easy is make the if/while/the_row stuff all one line, see below:
Start loops like this:
<?php if (have_rows('agenda_section_events')): while (have_rows('agenda_section_events')) : the_row(); ?>
End loops like this:
<?php endwhile; endif; ?>
You can go wild with the number of loops you want to nest, just simply copy the parent loop lines and create a nested one, then change your row to the nested one of course.
I adjusted your code below, give this a shot.
You should also define your subfield array values inside the loop you're using it with, just keeps things cleaner – or just use the_sub_field to echo your subfield values since its a short function anyway.
<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 ?>
这篇关于高级自定义字段无法在转发器内执行嵌套循环转发器 have_rows() 不执行任何操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文