【发布时间】:2021-10-05 06:44:07
【问题描述】:
这是对这个问题的跟进advanced custom fields can't do nested loops repeater inside repeater have_rows() not doing anything
现在我遇到了无法获取子字段 URL 的问题。
<div class="tc__agenda-speaker">
<?php while (have_rows('agenda_event_speakers')) : the_row(); ?>
<div class="tc__agenda-speaker-headshot">
<!-- DEBUG LINE -->
<div style="color: red;"><?php echo the_sub_field('agenda_event_speaker_headshot')['src'] ?></div>
<img src="<?php the_sub_field('agenda_event_speaker_headshot')['url'] ?>" alt="<?php the_sub_field('agenda_event_speaker_headshot')['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 ?>
这一行
<?php the_sub_field('agenda_event_speaker_headshot')['url'] ?>
它的输出是这样的
16835, 16835, Name color 2, Name-color-2.png, 152744, http://website.com/wp-content/uploads/2021/02/Name-color-2.png, http://website.com/post/post-title-here/Name-color-2/, , 86, , , Name-color-2, inherit, 16799, 2021-02-02 16:45:53, 2021-02-02 16:45:53, 0, image/png, image, png, http://website.com/wp-includes/images/media/default.png, 500, 500, Array
字段返回格式为数组
Advanced Custom Fields get sub field image
您可以使用 get_sub_field('imgcolumn_1')['url']; 获取图片的 URL
作为函数 get_sub_field() 返回一个数组。
此文档https://www.advancedcustomfields.com/resources/the_sub_field/ 表明访问索引没有区别
确实如此
这个函数和echo get_sub_field()本质上是一样的
如果我去
<?php echo the_sub_field('agenda_event_speaker_headshot')['url'] ?>
img 源是unknown
如何在高级自定义字段的子字段中访问图片数组的url索引?
【问题讨论】:
标签: php arrays wordpress advanced-custom-fields