Get image captions for images on gallery post format metabox

Get the attachment ID and convert to a post. From there the caption is stored on the post object.

$thumb_img = get_post( get_post_thumbnail_id() ); // Get post by ID

echo $thumb_img->post_excerpt; // Display Caption

echo $thumb_img->post_content; // Display Description

In your loop it would look like:

<?php if($gallery) : ?>
    <?php foreach($gallery as $key => $value) :
        $image = wp_get_attachment_image_src($value);
        $image_post = get_post($value);
        $caption = $image_post->post_excerpt;

How you want to output the value depends on you and your css expectations;

<p class="wp-caption-text"><?php echo $caption; ?></p>

An alternate would be to use Caption_Shortcode:

<image> Caption

Leave a Comment