Home › Forums › General Issues › adding a custom field to media attachment › Reply To: adding a custom field to media attachment
I have not tested this all out but it should work for you.
With a custom field image
<?php
$image = get_field('image');
$custom_image_url = get_field('customURL', $image['ID']);?>
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" data-attr="<?php echo $custom_image_url;?>" />
With a featured image
<?php
$url = get_the_post_thumbnail_url( $post->ID );
$thumbnail_id = get_post_thumbnail_id( $post->ID );
$custom_image_url = get_field('customURL', $thumbnail_id);
$alt = get_post_meta($thumbnail_id, '_wp_attachment_image_alt', true);
?>
<img src="<?php echo $url; ?>" alt="<?php echo $alt;?>" data-attr="<?php echo $custom_image_url;?>" />
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.