首页 > 解决方案 > 高级自定义字段 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 索引?

标签: phparrayswordpressadvanced-custom-fields

解决方案


Very simple - i had no image set in the field in the first place...

This is in fact the correct code.


推荐阅读