Skip Navigation

[Resolved] Repeatable Image Field > Image Caption in PHP

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.

Our next available supporter will start replying to tickets in about 8.39 hours from now. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 1 reply, has 2 voices.

Last updated by Waqar 9 months, 1 week ago.

Assisted by: Waqar.

Author
Posts
#2684225

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>';
}

#2684343

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

#2684427

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