Good evening, i created some custom fields for the images in the media gallery, and i need to print them with the images in my posts.
I have only been able to print them using the image id as a custom field.
<?php
$immagine= get_post_meta($post->ID, 'immagine', true);
$url = wp_get_attachment_url( $immagine );
?>
<img src="<?php echo $url; ?>" / >
<p><?php the_field('titolo_didascalia', $immagine); ?></p>
<p><?php the_field('formato', $immagine); ?></p>
<p><?php the_field('autore', $immagine); ?></p>
<p><?php the_field('latitudine', $immagine); ?></p>
<p><?php the_field('longitudine', $immagine); ?></p>
I’d like to know if there id a better way to do it and if the image gallery add on print custom attachent fields in the html of the website.
Thanks
No, this is the way you need to get fields from media, by supplying the post ID (attachment ID) when calling the_field(); https://www.advancedcustomfields.com/resources/how-to-get-values-from-another-post/
Thanks for the answer, but if i have a gallery how can i loop to get all the media custom fields? thanks
Looping over a gallery field is covered here https://www.advancedcustomfields.com/resources/gallery/. You just need to use the current image ID in the loop.