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;
}
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.