Support

Account

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;?>" />