I am using 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.
However, I am trying to display an image. I am using an adapted example from the acf documentation . 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:
Any suggestions what is wrong with my code?
I appreciate your replies!
3条答案
按热度按时间1l5u6lss1#
不要使用
the_field('box_image')['url']
,因为the_field()
直接回显所有内容,回显数组输出array()
;请尝试以下方法:hfwmuf9z2#
当你设置一个ACF,你可以选择选项“返回值”(数组,url,id)改变这作为id,如果你想得到不同的大小(缩略图),和URL,如果你想得到原始图像的网址。
如果使用ID选项将图像作为html标记,则可以使用
wp_get_attachment_image($id, $size);
。我总是倾向于只存储图像ID。dldeef673#
Every ACF field, including your image, has a 'Return Format' option, this option controls response of the
get_field()
function for that specific field. So, different return options will give different data and should be processed in different ways (e.g. an array with image sources or an image id).To avoid this hassle with return formats you can use an ACF Views plugin , it's a free plugin that allows to display any ACF fields (including images) simply using shortcodes, without coding at all. There you just select fields to display and get a shortcode, paste the shortcode in the target place and that's it. The plugin generates markup automatically and display fields, it saves a lot of time.
P.S. Here is a link to their official website - ACF Views: Display ACF fields and Posts . More information along with links to their YouTube tutorials and Docs can be found there.