0

I have a post format gallery. I have created a metabox to add my images:

function mytheme_show_post_gallery_metabox( $post ) {

wp_nonce_field( 'mytheme_post_gallery_metabox', '_mytheme_post_gallery_metabox' );
$gallery = get_post_meta( $post->ID, '_mytheme_post_gallery', true );

?>
<a class="gallery-add button media-button button-primary button-large media-button-select" href="#" data-uploader-title="<?php _e( 'Add gallery images', 'mytheme' ); ?>" data-uploader-button-text="<?php _e( 'Add gallery images', 'mytheme' ); ?>"><?php _e( 'Add gallery images', 'mytheme' ); ?></a>
<table>
  <tr>
    <td>
      <ul id="gallery-metabox-list">
        <?php if ( $gallery ) : ?>
          <?php foreach ( $gallery as $key => $value ) : $image = wp_get_attachment_image_src( $value ); ?>
            <li>
              <input type="hidden" name="_mytheme_post_gallery[<?php echo $key; ?>]" value="<?php echo $value; ?>">
              <img class="image-preview" src="<?php echo $image[0]; ?>">
              <a class="change-image" href="#"><?php _e( 'Change | ', 'mytheme' ); ?></a>
              <a class="remove-image" href="#"><?php _e( 'Remove', 'mytheme' ); ?></a>
            </li>
          <?php endforeach; ?>
        <?php endif; ?>
      </ul>
    </td>
  </tr>
</table>
<?php
}

This code allows me to add images to my gallery through a metabox. I am now trying to also get the caption for each image. I have been trying quite a few approaches but no luck so far.. Any ideas would be much appreciated.

0

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:

[caption]<image> Caption[/caption]
  • This works but only for the post thumbnail. I need to get the captions of the images I add to the post gallery on format post gallery through the code above. I get the images with wp_get_attachment_image_src() on this line of code <?php foreach ( $gallery as $key => $value ) : $image = wp_get_attachment_image_src( $value ); ?> I am wondering if there would be a similar function to get the caption of each gallery image. – Madeirense Dec 30 '15 at 17:04
  • Sorry, just one other thing. How would I then echo out this caption on the post page front end? – Madeirense Dec 30 '15 at 19:33
  • I mean the front end of the post. After I get the images on my metabox I show them in a gallery in the front end like this if ( !empty( $images ) ) { foreach ( $images as $image ) { echo '<li class="animated fadeIn">', '<figure>', wp_get_attachment_image( $image, 'post-full-width' ), '</figure>', '</li>'; } } I am just wondering how I would show the caption here. – Madeirense Dec 30 '15 at 19:47
  • I have asked it here wordpress.stackexchange.com/questions/213319/… – Madeirense Dec 30 '15 at 20:02

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.