Get advance custom field image


Get advance custom field image



i'm trying to get the object image that i inserted with acf. But the problem is: the code always return an array with only one index, that return a number (i think it's the id).



enter image description here





you can set the ACF field to either return an ID of the image, or the object.
– Stender
Jul 2 at 9:59





I set the ACF to return an object, but he always return an id
– D B
Jul 2 at 10:01





how you are calling the field in template?
– charan kumar
Jul 2 at 10:06





foto-d-5, then i use get_field('foto-d-5') in the code
– D B
Jul 2 at 10:08





$img = get_field('foto-d-5'); is returning an id?? it shouldn't!
– Stender
Jul 2 at 10:08


$img = get_field('foto-d-5');




2 Answers
2



Try setting the image field to return a URL, save the field group, set it back to "object" and save it again to see if works.



(I think somethimes the correct return format is not properly saved in the database for reasons I don't know)



If you are in development and it won't affect content already uploaded to that field, try deleting the field and recreating it.



This has happened to me to me from time to time during development and those "fixes" usually work.



Try this.


<?php

$image = get_field('foto-d-5');

if( !empty($image) ): ?>

<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />

<?php endif; ?>






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

api-platform.com Unable to generate an IRI for the item of type

How to set up datasource with Spring for HikariCP?

Display dokan vendor name on Woocommerce single product pages