php - 高级自定义字段 the_sub_field()['url'] 但它不返回 URL
问题描述
这是对这个问题的跟进高级自定义字段不能做嵌套循环 中继器内的中继器 have_rows() 没有做任何事情
现在我遇到了一个无法获取子字段 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
字段返回格式为数组
您可以使用 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 src 是unknown
如何在高级自定义字段的子字段中访问图像数组的 url 索引?
解决方案
Very simple - i had no image set in the field in the first place...
This is in fact the correct code.
推荐阅读
- sql - SQL-SSRS 报告
- django - Django Oracle OCA-24450
- python - for循环中的Python“else”和“if not”语句
- intellij-idea - IntelliJ IDEA 无法编译 groovy 测试
- doctrine-orm - 持久化数据后刷新时出现致命错误
- python - django crud 服务器总是返回 404
- reactjs - 使用 refs 在父 AJAX 之后设置子输入值
- java - 如何改善 SQL 连接
- angular - Angular 属性指令 - prod 构建错误,预期 1 个参数但得到 0
- elasticsearch - Windows 无法在本地启动弹性搜索服务