Hi there,
in the past I have used the code below to display the image galleries. I pulled the snippet from your support page and it works fine. This time I also need the image caption, I've already tried a few things but can't figure it out. Please help.
if (types_render_field( 'galerie') != ''){
echo '<div class="bildergallerie">';
$thumbs = types_render_field( 'galerie', array('size' => 'standard_thumb', 'separator' => '|', 'url' => true , 'post_id' => get_the_ID()) );
$thumbs = explode( '|', $thumbs );
$images = types_render_field( 'galerie', array('size' => 'large', 'separator' => '|', 'url' => true , 'post_id' => get_the_ID()) );
$images = explode( '|', $images );
foreach ($images as $index => $image) {
echo '<div class="galleriebild"> </div>';
}
echo '</div>';
}
Hi,
Thank you for contacting us and I'd be happy to assist.
In the code snippet, you'll need the full image URLs to get the image attachment IDs through the 'attachment_url_to_postid' function:
https://developer.wordpress.org/reference/functions/attachment_url_to_postid/
Next, those attachment IDs can be used with the function 'wp_get_attachment_caption', to get caption saved in the media library:
https://developer.wordpress.org/reference/functions/wp_get_attachment_caption/
Example:
if (types_render_field( 'galerie') != ''){
echo '<div class="bildergallerie">';
$thumbs = types_render_field( 'galerie', array('size' => 'standard_thumb', 'separator' => '|', 'url' => true , 'post_id' => get_the_ID()) );
$thumbs = explode( '|', $thumbs );
$images = types_render_field( 'galerie', array('size' => 'large', 'separator' => '|', 'url' => true , 'post_id' => get_the_ID()) );
$images = explode( '|', $images );
$full_images = types_render_field( 'galerie', array('size' => 'full', 'separator' => '|', 'url' => true , 'post_id' => get_the_ID()) );
$full_images = explode( '|', $full_images );
foreach ($images as $index => $image) {
$attachment_post_id = attachment_url_to_postid( esc_url( $full_images[ $index ] ) );
$attachment_caption = wp_get_attachment_caption($attachment_post_id);
echo '<div class="galleriebild"><a href="' . $image . '" class="lightbox" data-title="' . $attachment_caption . '"><img src="' . $thumbs[ $index ] . '" /></a></div>';
}
echo '</div>';
}
Note: The custom code examples from our forum are shared to get you started in the right direction. You're welcome to adjust them as needed and for more personalized customization assistance, you can consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/
regards,
Waqar
Hi Waqar,
thanks for the quick support.
I already suspected that this only works via the attachment_post_id. But I had no idea how to go about it.
Thank you for the finished code, works great!
BG Steve