Home Forums Front-end Issues How to Add Custom Meta Field To Media/Images


How to Add Custom Meta Field To Media/Images

  • I’m trying to add ‘Citation Author’ and ‘Citation URL’ fields to the images in the media gallery (see screenshot for setup). This part seems to be working as in the media gallery I have the option to add ‘Citation Author’ and ‘Citation URL’ to any image inside the media gallery.

    I’m having trouble figuring out how to retrieve these values from the image variable. Most of images are called like this:

    $image = get_field("hero_image");
    echo esc_url($image['sizes']['hero']);

    I have tried this but nothing shows:

    $image = get_field("hero_image");
    echo $image['citation_author'];
    echo $image['citation_url'];

    The examples I have seen all use the post Id or are based on the image being a featured image, which is not the case here. I have pages with sections that have ACF image fields and I’m calling them with get_field(“image_name”);

  • You need to get the fields for the image. They are not returned as part of what ACF normally returns. The post ID will be in either $image[‘id’] or $image[‘ID’].

    echo get_field('citation_author', $image['id']);
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.