WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

share|improve this question
auto answer. $attachments_caption[] = $attachment->post_excerpt; is working – rogaroga Oct 28 '11 at 18:19
Post that as an answer, rather than as a comment to your question - and then be sure to come back later and accept the answer as correct. – Chip Bennett Oct 28 '11 at 19:23

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:

get_post_field('post_excerpt', $attachment->ID);
share|improve this answer

You could try wp_prepare_attachment_for_js( $id ) and return everything you need for the attachment.

You'll receive an array with this:

  • id
  • title
  • filename
  • url
  • link
  • alt
  • author
  • description
  • caption
  • name
  • status
  • uploadedTo
  • date
  • modified
  • menuOrder
  • mime
  • type
  • subtype
  • icon
  • dateFormatted
  • nonces
  • editLink
  • sizes
  • width
  • height
  • fileLength
  • compat

Check the Codex: wp_prepare_attachment_for_js()

share|improve this answer
Welcome to WPSE. Link only answers aren't well recieved answers. Please post some relevant code/examples/description from the link, and keep the link for additional info and author credit – Pieter Goosen Aug 26 '14 at 12:52

This will fix your issue

$attachments = attachments_get_attachments();
$total_attachments = count( $attachments );
if( $total_attachments ){
 for( $i=0; $i<$total_attachments; $i++ ){
     echo $attachments[$i]['title']; 
     echo $attachments[$i]['caption'];
     echo $attachments[$i]['id'];
     echo $attachments[$i]['location'];
     echo $attachments[$i]['mime'];
     echo $attachments[$i]['filesize']; 
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.