Hi Luo,
I sent you a private message with the credentials to a live site.
The link you sent me:
https://toolset.com/forums/topic/how-to-retrieve-image-caption-alt-text-and-description/#post-227840
Is a couple of years older than the link sent by Supporter Jamal:
https://toolset.com/forums/topic/alt-text-output-for-images-from-custom-field/#post-1857657
The field is not repeating. Each image only has one credit value. I added the field through Custom Fields, and then I added them to the Post Type(s): Media.
These images can be added as a featured image, and I would like to show the content of the credit field. This has been resolved by Jamal. He then give me code that would also do that for images inserted in the body of a post, and that is not working.
I added the below code to functions.php:
function media_info_func( $atts ) {
// Attributes
$atts = shortcode_atts(
array(
'field' => '',
'info' => '',
),
$atts
);
global $post, $wpdb;
$field = $atts['field'];
$info = $atts['info'];
$image_url = get_post_meta( $post->ID, $field );
$attachment = $wpdb->get_col($wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE guid='%s';", $image_url ));
$attachment_id = $attachment[0];
if ( $field && $info ) {
switch($info) {
case 'id':
return $attachment_id;
break;
case 'title':
// return return ("[wpv-post-title item='$attachment_id']");
return wpv_do_shortcode("[wpv-post-title item='$attachment_id']");
break;
case 'alt':
// return ("[wpv-post-field name='_wp_attachment_image_alt' item='$attachment_id']");
return do_shortcode("[wpv-post-field name='_wp_attachment_image_alt' item='$attachment_id']");
break;
case 'caption':
// return ("[wpv-post-excerpt output='raw' item='$attachment_id']");
return do_shortcode("[wpv-post-excerpt output='raw' item='$attachment_id']");
break;
case 'description':
// return ("[wpv-post-body view_template='None' item='$attachment_id']");
return do_shortcode("[wpv-post-body view_template='None' item='$attachment_id' output='raw']");
break;
}
}
}
add_shortcode( 'media-info', 'media_info_func' );
And this is used in a Content Template:
[types field='wpcf-media-credit' item='[media-info field="wpcf-media-credit" info="id"]'][/types]
I even added a case for image-credit but that didn't do anything. It seems like the id is not available to be passed into the shortcode.
On the website, the best example post is:
hidden link
(here you see that for featured it works, but not for the rest)
The content template:
hidden link
The image:
hidden link
Here you see the Custom Fields for Media, Credit field.
Field slug is media-credit (wpcf-media-credit)