Skip Navigation

[Resolved] I need to display the repeating image title below the image

This thread is resolved. Here is a description of the problem and solution.

Problem:

The issue here is that the user wanted to display their custom image repeatable image fields and their titles.

Solution:

This can be done by using the code in the link below.
https://toolset.com/forums/topic/i-need-to-display-the-repeating-image-title-below-the-image/#post-1241867

This support ticket is created 5 years, 6 months ago. There's a good chance that you are reading advice that it now obsolete.

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
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 3 replies, has 2 voices.

Last updated by kristofG 5 years, 6 months ago.

Assisted by: Shane.

Author
Posts
#1241771

I am having trouble getting this to work. I have searched this forum and found a shortcode that displays the title, and it kinda works, except, it shows the same title for each image, rather than the specific title for each image.

/* SHORTCODE -- Get Repeating Field Title
==============================================*/
add_shortcode('get_media_title', 'get_media_title_func');

function get_media_title_func() {
global $post, $wpdb;
 
$images = get_post_meta($post->ID, 'wpcf-gallery', false);
foreach ($images as $image) {
$attachment_id = $wpdb->get_var($wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE guid = %s",$image));
$title = get_the_title($attachment_id);
}
return $title;
}

In my page Layout, I use

[wpv-for-each field="wpcf-gallery"]
<a rel="lightbox" data-rel="lightbox-gallery-1" href="[types field='gallery' size='full' url='true'][/types]"> [types field='gallery' alt='%%ALT%%' title='%%TITLE%%' size='custom' width='200' height='150' align='none' resize='crop' ][/types]</a>[get_media_title][/wpv-for-each]
#1241780

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Kristof,

Please provide the credentials so that I can test this solution out.

Thanks,
Shane

#1241867

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Kristof,

I must apologize for the delay, however here is the code that you will need.

/* SHORTCODE -- Get Repeating Field Title
==============================================*/
add_shortcode('get_media_title', 'get_media_title_func');
 
function get_media_title_func() {
global $post, $wpdb;
  
    // Attributes
  
    $image_urls = get_post_meta($post->ID, 'wpcf-image', false);
  
    $links = "";
  	$index = 0;
 	foreach ($image_urls as $image_url) {
 		if(!empty($image_url)){
	    		 $attachment = $wpdb->get_col($wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE guid='%s';", $image_url )); 
       
	    		 $links .= "<a rel='lightbox' data-rel='lightbox-gallery-1' href='".do_shortcode("[types field='image' size='full' index='".$index."' url='true'][/types]")."'>".do_shortcode("[types field='image' alt='%%ALT%%' title='%%TITLE%%' index='".$index."' size='custom' width='200' height='150' align='none' resize='crop' ][/types]")."</a>".get_the_title($attachment[0])."</a> ";
	    	 }
      	$index ++;
   	 	}
 	 	
 return $links; 
}

You wont need to use the for each shortcode because i've already done this in the code. So just use the shortcode [get_media_title] and it will return the images and the title as well.

Thanks,
Shane

#1242077

except for replacing wpcf-image with wpcf-gallery, the code works perfectly.
thanks a lot