Skip Navigation

[Resolved] Image id’s inside vc_gallery

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

Problem:

The issue here is that the user wanted to get the repeatable fields image id to render in the VC gallery shortcode.

Solution:

This can be done by using the shortcode below.

// Add Shortcode
function get_repeatable_image_ids( $atts ) { 
    // Attributes
    $atts = shortcode_atts(
        array(
            'field' => '',
            'id' => ''
        ),
        $atts
    );
    $image_urls = get_post_meta($atts['id'],$atts['field']);
    $id_list = array();
    foreach ($image_urls as $image_url) {
        if(!empty($image_url)){
                 array_push($id_list, attachment_url_to_postid($image_url));
             }
        }
     $gallery_ids = implode(",", $id_list); 
 return do_shortcode("[vc_gallery el_id='gallery-847486' medias='".$gallery_ids."' gutter_size='3' screen_lg='1000' screen_md='600' screen_sm='480' single_overlay_opacity='50' single_padding='2']");
   
}
add_shortcode( 'display_vc_gallery', 'display_vc_gallery' );

What I did was to render the VC shortcode directly in the custom shortcode. To use the shortcode you will need to do this.
[display_vc_gallery field='my-field' id="post-id"]

This support ticket is created 5 years, 11 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 12 replies, has 2 voices.

Last updated by paulw-2 5 years, 10 months ago.

Assisted by: Shane.

Author
Posts
#1196556

Hi Toolset,

I want to output the image id of a 'types image field - multiple-instances' for a slideshow.
The code i use to render the slideshow is:

[vc_row][vc_column width="1/1"][vc_gallery el_id="gallery-152540" medias="4570,4562,4554,4546" gutter_size="1" screen_lg="1000" screen_md="600" screen_sm="480" images_size="one-one" single_overlay_opacity="50" single_padding="2"][/vc_column][/vc_row]

On the position of medias we need the image id's - comma separated.

I found this post:
https://toolset.com/forums/topic/image-field-type-featured-image-how-can-i-display-image-title-alt-desc/
but cannot make it work for our solution. Can you please be so kind to help us out to achieve our goal?

Based on this post we placed the code in functions.php and created the next code slideshow code:
[vc_row][vc_column width="1/1"][vc_gallery el_id="gallery-152540" medias="[vc_gallery type="image_grid" images="[get_image_id url ="[types field='thumbnails' output='raw' ][/types]"]" img_size="300x222"]" gutter_size="1" screen_lg="1000" screen_md="600" screen_sm="480" images_size="one-one" single_overlay_opacity="50" single_padding="2"][/vc_column][/vc_row]

Unfortunately without luck.
Can somebody guide us to make this work? It would open a lot of doors in our website design...

Kind regards,

Paul

#1196771

Shane
Supporter

Languages: English (English )

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

Hi Paul.

Thank you for contacting our support forum.

I see that you are using the shortcode [types field='thumbnails' output='raw' ][/types] to get your image urls. Is this field a repeatable image field or just a single image field.

Secondly I see you are using this shortcode [get_image_id] is this a custom shortcode that was created to get the image id's based on their URL's?

Please let me know and we can proceed from there.

Thanks,
Shane

#1196919

Hi Shane,

At first I need to say I copied the wrong url as reference post.
This is the post (answered by you) I'm referring:
https://toolset.com/forums/topic/show-image-id/

I see that you are using the shortcode [types field='thumbnails' output='raw' ][/types] to get your image urls. Is this field a repeatable image field or just a single image field.
yes, its a repeatable field.

Secondly I see you are using this shortcode [get_image_id] is this a custom shortcode that was created to get the image id's based on their URL's?
As I described above, took the code from your suggestions there and adjusted it to the thing I need (our types image field is called: thumbnails)

I hope that the proper reference post url clears things out a bit more and you can help me out.

Thanks,

Paul

#1197352

Shane
Supporter

Languages: English (English )

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

Hi Paul,

Thank you for the post reference.

I modified this code so that it will get the ID's of a repeatable image field.

 // Add Shortcode
function get_repeatable_image_ids( $atts ) {
 global $wpdb;

    // Attributes
    $atts = shortcode_atts(
        array(
            'field' => '',
            'id' => ''
        ),
        $atts
    );

    $image_urls = get_post_meta($atts['id'],$atts['field']);
    $id_list = array();
 	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 )); 
	    		 array_push($id_list, $attachment[0]);
	    	 }
   	 	}
 	 	
 return implode(",", $id_list); 
 
}
add_shortcode( 'get_repeatable_image_ids', 'get_repeatable_image_ids' );

Add this to your Custom Code tab in Toolset -> Settings -> Custom Code and activate it.

You can use this shortcode by doing this [get_repeatable_image_ids field='wpcf-{customfieldslug}' id='[wpv-post-id]']

Now all you need to do is to add you repeatable image field slug, this will be inclusive of a wpcf- prefix. In my case it would be wpcf-image

Please let me know if this helps.

Thanks,
Shane

#1197421
Schermafbeelding 2019-02-07 om 17.55.16.png

Hi Shane,

I've tried and inserted your custom code in toolset.
In this panel there's a warning.

The shortcode is breaking as well, I've used this:
[vc_gallery el_id="gallery-847486" medias="[get_repeatable_image_ids field='wpcf-thumbnails' id='[wpv-post-id]']" gutter_size="3" screen_lg="1000" screen_md="600" screen_sm="480" single_overlay_opacity="50" single_padding="2"]

here is a front-end page: hidden link

Am I missing something?
(if you want I can give you access to development and content template views - please let me know)

Thanks so far!!!
Really appreciated.

#1197432

Shane
Supporter

Languages: English (English )

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

Hi Paul,

Yes please provide me with access so that I can check in more details on this for you.

Thanks,
Shane

#1197444

Shane
Supporter

Languages: English (English )

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

Hi Paul,

The code should be working fine now.

// Add Shortcode
function get_repeatable_image_ids( $atts ) { 
    // Attributes
    $atts = shortcode_atts(
        array(
            'field' => '',
            'id' => ''
        ),
        $atts
    );
    $image_urls = get_post_meta($atts['id'],$atts['field']);
    $id_list = array();
    foreach ($image_urls as $image_url) {
        if(!empty($image_url)){
                 array_push($id_list, attachment_url_to_postid($image_url));
             }
        }
         
 return implode(",", $id_list); 
  
}
add_shortcode( 'get_repeatable_image_ids', 'get_repeatable_image_ids' );

However it seems that this shortcode isn't accepting the custom shortcode as a parameter. Could you get in touch with the VC support to check if this can handle a shortcode to provide the data ?

Please check and let me know.

Thanks,
Shane

#1202923
Schermafbeelding 2019-02-20 om 09.16.50.png

Hi Shane,

I tired to get a answer by Uncode who delivered our theme.
They are not very helpful...

There is a section in vc composer where you can map shortcodes (see screenshot).
Would you mind take e look there and see what's possible?
It would be great to have this working...

Another thing is, when I place your shortcode without vc composer it's giving: '0,0,0,0,0,0,0'
Maybe there is something wrong there as well...

Hope you can find and help us with a solution.

Thanks,
Paul

#1203209

Shane
Supporter

Languages: English (English )

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

Hi Paul,

I can try to assist but I can't make any promises.

Would you mind allowing me to see the page where this is being loaded?

The private fields will be enabled for your next response.
Thanks,
Shane

#1204069

Shane
Supporter

Languages: English (English )

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

Hi Paul,

Not sure how this works entirely but it seems what we may need to render the shortcode for VC in the custom shortcode to work.

WOuld you like for me to set this up for you ? If this doesn't work as I would imagine it then we are really out of option.

Please let me know.
Thanks,
Shane

#1204299

Hi Shane,

It would be really great!
Hope you can get this to work..

Thanks,
Paul

#1204686

Shane
Supporter

Languages: English (English )

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

Hi Paul,

Good News 🙂

It works.

This is the final code now.

// Add Shortcode
function get_repeatable_image_ids( $atts ) { 
    // Attributes
    $atts = shortcode_atts(
        array(
            'field' => '',
            'id' => ''
        ),
        $atts
    );
    $image_urls = get_post_meta($atts['id'],$atts['field']);
    $id_list = array();
    foreach ($image_urls as $image_url) {
        if(!empty($image_url)){
                 array_push($id_list, attachment_url_to_postid($image_url));
             }
        }
     $gallery_ids = implode(",", $id_list); 
 return do_shortcode("[vc_gallery el_id='gallery-847486' medias='".$gallery_ids."' gutter_size='3' screen_lg='1000' screen_md='600' screen_sm='480' single_overlay_opacity='50' single_padding='2']");
  
}
add_shortcode( 'get_repeatable_image_ids', 'get_repeatable_image_ids' );

I just wrapped the shortcode in the custom shortcode I made so that it can start working with no issues. Since the VC shortcode doesn't accept shortcodes as parameter.

Please let me know if this helps.
Thanks,
Shane

#1209463

Hi Shane,

It works perfectly and duplicating and change the settings made it possible to create the options needed.
We now can have VC-sliders and VC-galleries in combination with wp-types / wp-views.

Thanks so much for your help... Really appreciated.
Kind regards,

Paul