Skip Navigation

[Resolved] Want to display captions under images

This support ticket is created 5 years, 4 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 – 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/Hong_Kong (GMT+08:00)

This topic contains 14 replies, has 3 voices.

Last updated by aaronT 5 years, 3 months ago.

Assisted by: Luo Yang.

Author
Posts
#1310147

I want to display caption under gallery images. Here is code for content template:

<h1>[wpv-post-title]</h1><br><br>

[wpv-for-each field="wpcf-photo"]


[/wpv-for-each]
CSS:
h1{text-align:center;}
img.object-fit {
margin: 5px;
width: 300px;
height: 300px;
border-radius: 15px;
object-fit: cover;
border: 1px solid;
}

#1310181

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Can you please try to use the following code to display the image caption. You should try to add the following code to your theme's functions.php file.
OR
You can add the following code to "Custom Code" section offered by Toolset:
=> https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/

add_shortcode('img-info', 'func_img_info');
function func_img_info($atts) {
global $post, $wpdb;
   
 
$attachment_id = $wpdb->get_var($wpdb->prepare(
"SELECT ID FROM $wpdb->posts WHERE guid=%s",
$atts['url']
));
 
 $caption = get_the_excerpt($attachment_id);

return $caption;
}

You can call the above shortcode as given under:

[img-info url='[types field='photo' output='raw'][/types]']
#1312855

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

When I try to load the site link you shared: hidden link

But its not loading and I see the message "This site can’t be reached".

Can you please send me a working site link.

#1313743

Sorry, the site is hidden link

#1314177

Hello,

Minesh is on vacation, I will take care of this thread.
I cam log into your website, but it is not an administrator account.

Since it is a custom codes problem, please provide below details:
1) Valid administrator account + FTP access
2) Where and how can I see the problem? please point out the problem page URL and where I can edit your PHP codes,
I need to test it in a live website, thanks

#1314325

Dear Aaron,

I am not sure where I can edit your PHP codes, but I can see the content template settings:
hidden link

You can try to modify the PHP codes as below:

add_shortcode( 'show_file_info', 'func_show_file_info');
function func_show_file_info($atts) {
    
    global $wpdb;
    $atts = shortcode_atts( array(
        'url' => '',
        'info' => '', // title, alt or id return
    ), $atts);
	var_dump($atts);
    $res = '';
    $url = $atts['url'];
    $attachment_id = $wpdb->get_var($wpdb->prepare(
        "SELECT ID FROM $wpdb->posts WHERE guid = %s", $url
    ));
    if($atts['info'] == 'title'){
        $res = get_the_title($attachment_id);
    }
    if($atts['info'] == 'alt'){
        $res = get_post_meta($attachment_id, '_wp_attachment_image_alt', true);
    }
    if($atts['info'] == 'id'){
        $res = $attachment_id;
    }
    if($atts['info'] == 'caption'){
        $res = get_the_excerpt($attachment_id);
    }
    return $res;
}

And use above shortcode as below:

[show_file_info info="caption" url="[types field='photos1' output="raw"][/types]"]

If you want to use it into HTML img tag, you can try this:

<img class="object-fit" src="[types field='photos1' output="raw"][/types]" title='[show_file_info info="caption" url="[types field='photos1' output="raw"][/types]"]'></a>

I have tested above codes in my localhost, it works fine.

#1315985

Luo,
You said you had this working on your local computer. Can you send me screen shots of where you put the codes.

Thanks,
Aaron

#1315989
content-template.JPG
functions.JPG

I put those custom PHP codes into my theme file "functions.php", and display those shortcode in a content template, here are screenshots.

If you need assistance for it, please point out where I can edit the custom PHP codes in your website, thanks

#1316051

Luo,
I tried to add php codes to functions.php and got an error on the first line of code. Can you please try for me? I'm trying on a different website.
The Website is hidden link
Password is tralex61####
This website has 2 galleries. one is Oceanwalk and the other is seaside. If you go to oceanwalk info in the menu and go a little bit down the page you will see where I have the gallery. It is called with the content template. The only difference in these galleries is that the field is photo. I'm using the Astra theme, so if you go to appearance and themes you will see theme editor where you can add code to functions.php. Thanks, Aaron

#1316053

Forgot, User name is testuser1

#1316081

Thanks for the details, I can log into your website, will update here if there is any found

#1316085

I have done below modifications in your website:
1) Put below PHP codes into your theme file "functions.php", line 142~167:

add_shortcode( 'show_file_info', 'func_show_file_info');
function func_show_file_info($atts) {
     
    global $wpdb;
    $atts = shortcode_atts( array(
        'url' => '',
        'info' => '', // title, alt or id return
    ), $atts);
	
    $res = '';
	$url = $atts['url'];
	$attachment_id = attachment_url_to_postid( $url );
    if($atts['info'] == 'title'){
        $res = get_the_title($attachment_id);
    }
    if($atts['info'] == 'alt'){
        $res = get_post_meta($attachment_id, '_wp_attachment_image_alt', true);
    }
    if($atts['info'] == 'id'){
        $res = $attachment_id;
    }
    if($atts['info'] == 'caption'){
        $res = get_the_excerpt($attachment_id);
    }
    return $res;
}

2) Edit the content template "Template for Photo Galleries", display the image file caption and title with above custom shortcode, line 7~8:
hidden link

caption: [show_file_info info="caption" url="[types field='photo' output="raw"][/types]"]
title: [show_file_info info="title" url="[types field='photo' output="raw"][/types]"]

Test it in front-end:
hidden link

It works fine.

Notice: you did not setup any caption for those image files, for example:
hidden link
in the input box "Caption" is empty.

#1316233

It works. How do I put the caption under the images?

Thanks,
Aaron

#1316259

That is a custom CSS codes problem, I have done below modification in your website:
Edit the content template:
hidden link
1) Modify the codes as below:

<h1>[wpv-post-title]</h1><br><br>

[wpv-for-each field="wpcf-photo"]
<div class="single-image">
<a href="[types field='photo' output="raw" ][/types]" class="lightbox" rel="gallery">
<img class="object-fit" src="[types field='photo' output="raw"][/types]" title="">
</a>
<div class="image-caption">[show_file_info info="caption" url="[types field='photo' output="raw"][/types]"]</div>
</div>
[/wpv-for-each]

2) Click "CSS Editor", add below CSS codes:

.single-image{
display:inline-table;
}

More help:
hidden link

#1316851

My issue is resolved now. Thank you!