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 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.