Home › Forums › General Issues › Function to Return Image Caption from ID not Image Object? › Reply To: Function to Return Image Caption from ID not Image Object?
Ok, after much gnashing of teeth and sighing I finally got something that worked. Immediately the problem is that wp_get_attachment_metadata is for something else. I wanted wp_get_attachment
Function:
function wp_get_attachment( $attachment_id ) {
$attachment = get_post( $attachment_id );
return array(
'alt' => get_post_meta( $attachment->ID, '_wp_attachment_image_alt', true ),
'caption' => $attachment->post_excerpt,
'description' => $attachment->post_content,
'href' => get_permalink( $attachment->ID ),
'src' => $attachment->guid,
'title' => $attachment->post_title
);
}
Useage:
<?php
$attachment = wp_get_attachment( get_field( 'primary_image' ) );
echo $attachment['caption'];
?>
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.