I am displaying the attachments on the parent post page with this code :
$args = array('post_type' => 'attachment', 'post_mime_type' => 'image', 'order'=> 'ASC', 'numberposts' => -1, 'post_status' => null, 'post_parent' => $post->ID );
$attachments = get_posts($args);
if ($attachments) {
foreach ( $attachments as $attachment ) {
$attachments_url[] = $my_image;
$attachments_caption[] = get_the_excerpt();
}
}
problem is that the excerpt doesn’t get the attachment’s caption but the post excerpt.
do you know how to display the attachment’s captions ?
thank you
get_the_excerpt()
should work for getting caption just fine.Your issue is that it looks for post to process in global variables and in your code there you are not setting it up with attachments you are iterating through.
You need to use
setup_postdata()
for it to work.Other way would be something like:
You could try
wp_prepare_attachment_for_js( $id )
and return everything you need for the attachment.You’ll receive an array with this:
Check the Codex: wp_prepare_attachment_for_js()
This will fix your issue