Skip Navigation

[Resolved] Custom shortcode for Image ID

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

Problem:

Display image IDs of repeating image field with shortcode.

Solution:

It needs custom codes, for example:

https://toolset.com/forums/topic/custom-shortcode-for-image-id/#post-1686271

Relevant Documentation:

This support ticket is created 3 years, 9 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.

Our next available supporter will start replying to tickets in about 1.98 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/Hong_Kong (GMT+08:00)

This topic contains 4 replies, has 3 voices.

Last updated by michaelM-30 3 years, 9 months ago.

Assisted by: Luo Yang.

Author
Posts
#1685455

We are using WPBakery for some of our Frontend displays - like galeries etc.
In order to call the custom type Images from Toolset we already had help from you guys with a script in the function.php and some shortcode, that converts the image to an ID - which is required for WPBakery.

But since one of the last updates of WP or toolset, the same shortcode isn't showing any images in the frontend.
I can get it to work - if I use the Image Ids, seperated by comma and input them by hand ([1234,1235,1236...])

This was the ticket, that helped us set our shortcode up:
https://toolset.com/forums/topic/using-images-from-toolset-fields-with-wpbakery-elements/

and this was the script for the function.php from Luo Yang-
add_shortcode('media-info', 'media_info_func');
function media_info_func($atts, $content){
global $wpdb;
$atts = shortcode_atts( array(
'slug' => 'photos',
'info' => 'title',
), $atts);
$res = '';
$url = types_render_field($atts['slug'], array('raw' => true));
$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;
}
return $res;
}

Has something changed in the latest wordpress or toolset updates?
Sorry I am no programmer.

Thank
Michael

#1686271

Waqar
Supporter

Languages: English (English )

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

Hi Michael,

Thanks for asking! I'd be happy to help.

As we discussed in our previous thread on this topic, if the image field is set to have multiple instances, then the second custom shortcode from Luo's thread should be used:
( ref: https://toolset.com/forums/topic/image-field-id/#post-277416 )


add_shortcode('media-info-multi', 'media_info_multi_func');
function media_info_multi_func($atts, $content){
    global $wpdb;
    $atts = shortcode_atts( array(
        'slug' => 'photos',
        'info' => 'title',
    ), $atts);
    $res = '';
    $urls = types_render_field($atts['slug'], array('raw' => true, 'separator'=> ','));
    $arr = explode(',', $urls);
    $attachment_id = array();
    foreach($arr as $k => $v){
        $attachment_id[] = $wpdb->get_var($wpdb->prepare(
            "SELECT ID FROM $wpdb->posts WHERE guid = %s", $v
        ));
    }
    $res = implode(',', $attachment_id);
    return $res;
}

I've tested it on my website and it is still working as expected to return the image/attachment post IDs, separated by commas.

In case the issue still persists, you're welcome to share temporary admin login details along with the example page where this shortcode is not working.

Note: Your next reply will be private and please make a complete backup copy, before sharing the access details.

regards,
Waqar

#1691185

Hello,

Waqar is on vacation, I will take care this thread.

I can login your website, please check the problem post here:
hidden link

in the image field "Dokument-Bild", you did not select any image in all items.
I have added one item at the end, and select an image from media library, test it in front-end:
hidden link

It works fine.

So you will need choose an image for each item in the image field "Dokument-Bild" and test again.

#1691243

That's odd, as I have selected images - but as you propably saw had no thumbnails in the backend.
Thats why they could not be called by the script.
I will have to investigate why the loaded images sometimes have not thumbnails.
Thanks for the moment.

#1691245

My issue is resolved now. Thank you!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.