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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. 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 2 months 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"><a href="' . $image . '" class="lightbox"><img src="' . $thumbs[ $index ] . '" /></a></div>';
}

echo '</div>';
}

#2684343

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

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

stefanP-4 confirmed that the issue was resolved on 2024-02-21 15:04:48.
This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.