Skip Navigation

[Resolved] Displaying Caption from Media Post

This thread is resolved. Here is a description of the problem and solution.

Problem: I am showing a View of Media posts. In that View, I would like to display the caption associated with the post.

Solution: Use this custom shortcode to access a Media post's caption:

add_shortcode( 'toolset_get_image_caption', 'toolset_get_image_caption_func');
function toolset_get_image_caption_func($atts)
{
  global $wpdb;
  $url = $atts['url'];
  $id = $wpdb->get_var( $wpdb->prepare ("SELECT ID FROM $wpdb->posts WHERE guid=%s ORDER BY `ID` DESC", $url ) );
  $excerpt = get_the_excerpt($id);
  if($excerpt)
    return $excerpt;
  $attachment_meta = get_post_meta( $id, '_wp_attachment_metadata', true );
  if (isset($attachment_meta['image_meta']))
    return $attachment_meta['image_meta']['caption'];
}

Add that code to your child theme's functions.php file. Then in a loop of Media posts, you can use the shortcode like this:

[toolset_get_image_caption url="[wpv-post-url]"]
This support ticket is created 6 years, 2 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 5 replies, has 3 voices.

Last updated by Peter 6 years, 2 months ago.

Assisted by: Christian Cox.

Author
Posts
#612769

Hi,

In the code below from "Loop Item" in "Templates for this View", I have made a custom field, 'picture-title', because I can't figure out a way to access the Caption metadata from the Media image's attachment data. This is obviously not ideal, as the user will have to populate both Caption and Picture Title with the same information (galleries and light boxes will use Caption, whereas my screen would need Picture Title if I can't resolve this - not ideal!)

I thought I had stumbled on a rather obvious answer where I can insert Post Featured Image a second time, but this time select "Caption of the Image" from the "What to display" drop-down, giving me this short code: [wpv-post-featured-image output="caption"]. However, no output is produced.

I read this and related articles (https://toolset.com/forums/topic/caption-for-repeatable-images-field/), so I think there is a problem in this area. I have tried to follow the workaround, but without success.

So, to summarise, what custom function do I need, and what would I replace [types field='picture-title'][/types] with below so I could actually get rid of that field, and just reference each image's Caption.

CODE
<div class="row">
<div class="col-sm-12">[wpv-post-featured-image size="medium"]</div>
</div>
<div class="row">
<div class="col-sm-12">[types field='picture-title'][/types]</div>
</div>

Thanks very much for your help.

Steve

#612933

Hi, I'm not quite sure I understand what type of image you are describing. Are you trying to show the post's featured image and its caption, are you trying to show an image stored in a post's custom field and that image's caption, or are you trying to show multiple images stored in a single repeating custom field and the captions of each of those images? The procedure is different for each of these cases.

#612989
Screenshot (247) selected post type.png
Screenshot (248) insert post featured image into Loop Item.png
Screenshot (249) post featured image shortcode in Loop Item.png
Screenshot (250) output of images found in the loop.png

Hi Christian,

Thanks for coming back to me.

I am looping through posts of type "Media", and picking up "featured image". I want the Captions from those to display under each one.

I've attached some screen shots which should help.

Many thanks.

Steve

#613016

Okay I have this custom shortcode you can use to access a Media post's caption:

add_shortcode( 'toolset_get_image_caption', 'toolset_get_image_caption_func');
function toolset_get_image_caption_func($atts)
{
  global $wpdb;
  $url = $atts['url'];
  $id = $wpdb->get_var( $wpdb->prepare ("SELECT ID FROM $wpdb->posts WHERE guid=%s ORDER BY `ID` DESC", $url ) );
  $excerpt = get_the_excerpt($id);
  if($excerpt)
    return $excerpt;
  $attachment_meta = get_post_meta( $id, '_wp_attachment_metadata', true );
  if (isset($attachment_meta['image_meta']))
    return $attachment_meta['image_meta']['caption'];
}

Add that code to your child theme's functions.php file. Then in a loop of Media posts, you can use the shortcode like this:

[toolset_get_image_caption url="[wpv-post-url]"]
#613053

Thank you Christian. That works perfectly :)!

Best wishes,

Steve

#636728

I had exactly the same issue, so thanks from me too, Peter

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.