ACF Image Field,从数组中获取单个值,而不是数组

ACF Image Field, get single values from array, instead of array

我正在使用 Advanced Custom Fields PRO Version 5.3.7。我正在尝试加载属于我的自定义 post 类型的图片。

我已经为自定义 post 类型配置了 ACF 字段,它适用于与文本相关的字段。

但是,我正在尝试显示图像。我正在使用 acf documentation 中的改编示例。您可以在下面找到我的代码。

<div class="image">
    <a href="<?php the_field('link'); ?>" rel="nofollow" class="am" target="_blank" > <img class="b-lazy wp-post-image b-loaded" alt="<?php echo the_field('box_image')['alt']; ?>" src="<?php echo the_field('box_image')['url']; ?>" width="230"> </a>
</div>

我的问题是,在我的站点上,我取回了数组而不是单个值:

有什么建议我的代码有什么问题吗?

感谢您的回复!

当你设置 ACF 时,你可以 select options "Return value" (array, url, id) 如果你想获得不同的大小(缩略图),将其更改为 id,并且URL 如果你想获取原始图像 url。

如果您使用 ID 选项将图像作为 html 标签获取,您可以使用 wp_get_attachment_image($id, $size); 我总是更喜欢只存储图像 ID。

不要使用 the_field('box_image')['url'] 因为 the_field() 直接回显所有内容并回显数组输出 array();而不是这个,试试这个:

<?php $img = get_field('box_image'); ?>
<?php if( $img ): ?>
  <a href="<?php the_field('link'); ?>" rel="nofollow" class="am" target="_blank" >
    <img class="b-lazy wp-post-image b-loaded" alt="<?php esc_attr_e( $img['alt'] ); ?>" src="<?php echo esc_url( $img['url'] ); ?>" width="230">
  </a>
<?php endif; ?>