Home › Forums › General Issues › Attachment field – how to output in template? › Reply To: Attachment field – how to output in template?
I thought there could be a ACF function or something. Now I did it with a normal custom field and put it to the caption. For this I had to disable the caption and made a custom caption:
// add copyright to caption function
//////////////////////////////////////////////////////////
// add custom field to image editor
add_filter("attachment_fields_to_edit", "add_image_copyright", 10, 2);
function add_image_copyright($form_fields, $post) {
$form_fields["copyright"] = array(
"label" => __("Copyright"),
"input" => "text",
"value" => get_post_meta($post->ID, "copyright", true),
"helps" => __("(Das \"©\" Zeichen wird automatisch hinzugefügt)"),
);
return $form_fields;
}
add_filter("attachment_fields_to_save", "save_image_copyright", 10 , 2);
function save_image_copyright($post, $attachment) {
if (isset($attachment['copyright']))
update_post_meta($post['ID'], 'copyright', $attachment['copyright']);
return $post;
}
// caption function for regular caption and copyright field
// disable regular caption and build a custom one
add_filter( 'disable_captions', create_function('$a', 'return true;') );
function image_send_to_editor_2($html, $id, $caption, $title, $align, $url, $size, $alt) {
$width = 'auto';
if ( preg_match( '/width="([0-9]+)/', $html, $matches ) ) {
$width = $matches[1] . 'px';
}
// Extract attachment $post->ID
preg_match('/\d+/', $id, $att_id);
if (is_numeric($att_id[0]) && $copyright = get_post_meta($att_id[0], 'copyright', true)) {
$cright .= '<span class="caption-customfield">© ' . $copyright . '</span>';
}
$output = '[caption id="attachment_' . $id . '" align="align' . $align . '" width="' . $width . '"]';
$output .= $html;
$output .= $cright.'<span class="caption-regular">'.$caption.'</span>'.'[/caption]';
return $output;
}
add_filter('image_send_to_editor', 'image_send_to_editor_2', 10, 8);
This is the output:
<div id="attachment_1312" style="width: 310px" class="wp-caption alignleft">
<img class="alignleft size-medium wp-image-1312" src="http://localhost:8888/mywebsite/wp-content/uploads/2015/09/download102-300x225.jpg" alt="download10" height="225" width="300">
<p class="wp-caption-text">
<span class="caption-customfield">© Name of Photographer</span>
</p>
</div>
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.