[英]ACF Image Field, get single values from array, instead of array
I am using Advanced Custom Fields PRO Version 5.3.7
. 我正在使用
Advanced Custom Fields PRO Version 5.3.7
。 I am trying to load a picture that is within my custom post type. 我正在尝试加载自定义帖子类型内的图片。
I have configured the ACF field for the custom post type and it works well for text related fields. 我已经为自定义帖子类型配置了ACF字段,它对于与文本相关的字段非常有效。
However, I am trying to display an image. 但是,我正在尝试显示图像。 I am using an adapted example from the acf documentation .
我正在使用acf文档中的改编示例。 Below you can find my code.
在下面可以找到我的代码。
<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>
My problem ist that on my site, I get the Array back instead of the single value: 我的问题是,在我的网站上,我得到了Array而不是单个值:
Any suggestions what is wrong with my code? 有什么建议我的代码有什么问题吗?
I appreciate your replies! 感谢您的答复!
Whn you setting a ACF, you can select options "Return value" (array, url, id) change this as id if you want get different sizes (thumbnails), and URL if you want get original image url. 设置ACF时,可以选择“返回值”(数组,URL,ID)选项,如果要获得不同的大小(缩略图),则可以将其更改为ID;如果要获得原始图像URL,则可以将其更改为URL。
if you use ID option to get image as html tag you can use wp_get_attachment_image($id, $size);
如果使用ID选项将图像获取为html标签,则可以使用
wp_get_attachment_image($id, $size);
i always prefer to store only image ID. 我总是喜欢只存储图像ID。
Dont use the_field('box_image')['url']
because the_field()
echoes directly everything and echoing an array outputs array()
; 不要使用
the_field('box_image')['url']
因为the_field()
直接回显所有内容,并且回显数组会输出array()
; Instead of this, try this: 代替这个,试试这个:
<?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; ?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.