Home › Forums › Add-ons › Gallery Field › Adding image data to gallery field images?
I need to add a “copyright” information to each image in my ACF gallery. So I added an extra field to the “edit attachment” screen using this wordpress code from the wordpress codex:
// Images custom fields
function my_add_attachment_copyright_field( $form_fields, $post ) {
$field_value = get_post_meta( $post->ID, 'copyright', true );
$form_fields['copyright'] = array(
'value' => $field_value ? $field_value : '',
'label' => __( 'copyright' ),
'helps' => __( 'Copyright Hinweis' )
);
return $form_fields;
}
add_filter( 'attachment_fields_to_edit', 'my_add_attachment_copyright_field', 10, 2 );
function my_save_attachment_copyright( $attachment_id ) {
if ( isset( $_REQUEST['attachments'][$attachment_id]['copyright'] ) ) {
$copyright = $_REQUEST['attachments'][$attachment_id]['copyright'];
update_post_meta( $attachment_id, 'copyright', $copyright );
}
}
add_action( 'edit_attachment', 'my_save_attachment_copyright' );
I can grab this new information in my template for the post-thumbnails this way:
© <?php echo get_post_meta(get_post_thumbnail_id(), "copyright", true);?>
This works, nice.
But how can i push this information to the images in my ACF gallery field? This is my code for the gallery (works with fancybox 2):
<?php $gallery++; ?>
<?php foreach( $rows as $image ): ?>
<?php for($i = 0; $i < count($gallery); $i++) { ?>
<!-- thumbnails -->
<?php
echo '<a href="'. $image['sizes']['fullheight'].'" rel="gallerygroup-'.$gallery.'" data-title-id="title-'.$num.'" class="col-xs-3 col-sm-2 fancybox">';
echo '<figure><img src="'.$image['sizes']['gal-thumb'].'" alt="'.$image['alt'].'"/>
<span class="icon-zoom-in icon-20 white" aria-hidden="true"></span>
</figure>';
echo '</a>';
echo '<div id="title-'.$num++.'" class="hidden">'.$image['title'].' <span class="gal-copyright">(© '.get_post_meta( $post->ID, 'copyright', true ).')</span></div>';
?>
<?php } ?>
<?php endforeach; ?>
But the copyright won’t show, is this code wrong?
get_post_meta( $post->ID, 'copyright', true )
Unfortunately <?php var_dump(get_field('gallery')) ?>
gives me “NULL”.
LOL, replacing
get_post_meta( $post->ID, 'copyright', true )
with
get_post_meta( $image['id'], 'copyright', true )
did the trick.
But why does <?php var_dump(get_field('gallery')) ?>
not work?
I need to add a custom taxonomy(image-tag and image-category) information to each image in my ACF gallery. How can I do?
I’m assuming that you’ve already assigned the custom taxonomies to the attachment post type. What you need to do is attach the fields to the media, not to the gallery, in other words add a field group with the location rule Attachemnt
is equal to
All
. Unfortunately, there isn’t any way to limit the fields to just images when using the WP popup media library view.
The topic ‘Adding image data to gallery field images?’ is closed to new replies.
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.