Hello I've been struggling for ages trying to find a solution for this. Basically I just want to retrieve all images from a post and display a caption underneath. Here is the code i'm using:

$argsThumb = array(
    'order'          => 'DESC',
    'post_type'      => 'attachment',
    'post_parent'    => $post->ID,
    'post_mime_type' => 'image',
    'post_status'    => null
$attachments = get_posts($argsThumb);
if ($attachments) {
    foreach ($attachments as $attachment) {
        echo '<div class="slideshow-content"><img src="'.wp_get_attachment_url($attachment->ID, 'thumbnail', false, false).'" /><div class="captions"><p>'.apply_filters('the_title', $attachment->post_title).'</p></div></div>';

As you can see it currently displays the title, but not the caption. I have tried using: $caption = $attachment->post_excerpt; - but not exactly sure where to implement it and what I have tried doesn't work. Any help would be greatly appreciated!

  • 1
    Why $attachment->post_excert; doesn't work for you? – Mamaduka Sep 23 '11 at 13:09

the following should just show the caption:

echo '<div class="slideshow-content"><img src="'.wp_get_attachment_url($attachment->ID, 'thumbnail', false, false).'" /><div class="captions">'.apply_filters('the_excerpt', $attachment->post_excerpt).'</div></div>';

(applying the excerpt filter adds the paragraph tag automatically)

| improve this answer | |
  • Thanks Michael, that's fixed it. The only problem I have is that when first uploading an image and a caption is entered it displays fine. However, if upload without a caption & then edit the post and image caption, it won't display. Is there a fix for this? – Tim Sep 23 '11 at 14:47
  • Actually, no worries I have solved it. Editing the caption from the gallery works, but not from within the post editor! – Tim Sep 23 '11 at 14:57
  • There's no way to access the Post Editor for Attachment post-types. As far as I know, you have to use the Media Manager. The "caption" field correlates to $post->post_excerpt for the Attachment itself. The Post Editor applies to the excerpt for the Parent Post to which the images are attached. – Chip Bennett Sep 23 '11 at 15:38
  • Of course - that's why it wasn't updating output! Thanks. – Tim Sep 23 '11 at 16:26

In what context are you attempting to run this code?

If it's inside the Loop (e.g. within another Post), why not just use [gallery id="123"]?

If it's outside the Loop (or outside the Post content), why not use do_shortcode( 'gallery', $args )?

| improve this answer | |
  • I'm running it inside the loop in single.php. I need to load images with <div class="slideshow-content"> wrapped around for a slideshow. Is this also possible with the method you mentioned? – Tim Sep 23 '11 at 14:50
  • Is this slideshow going to appear in every Post, or just selected Posts. If only in selected Posts: how are you implementing the selection? – Chip Bennett Sep 23 '11 at 15:37
  • Currently it is every post - as I am creating the theme to be used as a portfolio. However - I may separate it later by category - I'm assuming something like this would work? <?php if ( in_category( 'slideshow' )) { // do slideshow } else { // other } ?> – Tim Sep 23 '11 at 16:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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