Support

Account

Home Forums General Issues adding a custom field to media attachment Reply To: adding a custom field to media attachment

  • I thought you were including it in a template not looking for a filter.

    If you are using an ACF image field on your page to include the image or including the image in your content you will have to build it into your template or create a short code.

    If you are using WordPress’ Featured Image system you can include the following and changing your custom field accordingly.

    
    add_filter('post_thumbnail_html', 'my_thumbnail_filter_method', 10, 5 );
    function my_thumbnail_filter_method($html, $post->ID, $post_thumbnail_id, $size, $attr) {
        $id = get_post_thumbnail_id();
        $src = wp_get_attachment_image_src($id, $size); 
        $alt = get_the_title($id);
        $class = $attr['class'];
    	$attr = get_field('CUSTOM FIELD NAME',$post->ID);
        $html = '<img src="' . $src[0] . '" alt="' . $alt . '" class="' . $class . '" data-attr="" />';
        return $html;
    }