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
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
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.
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.
My issue is resolved now. Thank you!