Skip Navigation

[Resolved] Get images ids for wordpress gallery

The Toolset Community Forum is closed, for technical support questions, please head on to our Toolset Professional Support (for paid clients), with any pre-sale or admin question please contact us here.

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

Problem:
I need help to get the images ids from repeatable field to create WordPress gallery to have like this shortcode:

[gallery ids="175,174,171,172"]
Solution:
There isn't such a feature within Types plugin, see our document:
https://toolset.com/documentation/customizing-sites-using-php/functions/#image
There isn't such an option can output the attachment image ID as the result.
And Types image field stores the image URL in the database.

I suggest you create another shortcode for it, see wordpress document:
http://codex.wordpress.org/Function_Reference/add_shortcode

For example:
1) Add below codes into your theme/functions.php

add_shortcode('repeating-images-gallery', 'repeating_images_gallery_func');
function repeating_images_gallery_func($atts, $content) {
    global $post, $wpdb;
      
    $images = get_post_meta($post->ID, 'wpcf-my-slide', false);
    $ids = array();
    foreach ($images as $image) {
        $attachment_id = $wpdb->get_var($wpdb->prepare(
            "SELECT ID FROM $wpdb->posts WHERE guid = %s",
        $image
        ));
        $ids[] = $attachment_id;
    }
    $out = do_shortcode('[gallery ids="' . implode(",", $ids) . '"]');
    return $out;
}

2) Put the gallery shortcode like this:
[repeating-images-gallery]
And we have [wpv-for-each] shortcode, you can use it to display all images in the repeatable field
Relevant Documentation:
https://toolset.com/documentation/views-shortcodes/#wpv-for-each

This is the community support forum for Types plugin, which is part of Toolset. Toolset is a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients and people who registered for Types community support can post in it.

This topic contains 4 replies, has 2 voices.

Last updated by saidB1322 5 years, 3 months ago.

Assigned support staff: Luo Yang.

Author
Posts
#409682

hello
I need help to get the images ids from repeatable field to create WordPress gallery to have like this shortcode:

[gallery ids="175,174,171,172"]

thanks

#409979

Luo Yang
Supporter

Languages: English (English ) Chinese (Simplified) (简体中文 )

Timezone: Asia/Hong_Kong (GMT+08:00)

Dear Said,

There isn't such a feature within Types plugin, see our document:
https://toolset.com/documentation/customizing-sites-using-php/functions/#image
There isn't such an option can output the attachment image ID as the result.
And Types image field stores the image URL in the database.

I suggest you create another shortcode for it, see wordpress document:
http://codex.wordpress.org/Function_Reference/add_shortcode

And we have [wpv-for-each] shortcode, you can use it to display all images in the repeatable field
https://toolset.com/documentation/views-shortcodes/#wpv-for-each

Hope it help

#410008

Thank you Mr Luo Yang

Can you help me create gallery from repeatable image field. i saw many issues in your forum but can't find a solution , the ACF plugin have a special field for gallery , i suggest you think about adding such a field to Types .
I am working with the newspaper theme and it has a special gallery the shortcode is like this :

 [gallery td_select_gallery_slide="slide" ids="170,172,171,164,163"] 

i achieved it with ACF with this code :

$image_ids = get_field('gallery', false, false);

$shortcode = '[gallery td_select_gallery_slide="slide" ids="' . implode(',', $image_ids) . '"]';

echo do_shortcode( $shortcode ); 

Any help to achieve this with types ?

Thank you

#410267

Luo Yang
Supporter

Languages: English (English ) Chinese (Simplified) (简体中文 )

Timezone: Asia/Hong_Kong (GMT+08:00)

For example, your custom repeatable image field is using slug "my-image", please try this:
1) Add below codes into your theme/functions.php

add_shortcode('repeating-images-gallery', 'repeating_images_gallery_func');
function repeating_images_gallery_func($atts, $content) {
	global $post, $wpdb;
	 
	$images = get_post_meta($post->ID, 'wpcf-my-slide', false);
	$ids = array();
	foreach ($images as $image) {
		$attachment_id = $wpdb->get_var($wpdb->prepare(
			"SELECT ID FROM $wpdb->posts WHERE guid = %s",
		$image
		));
		$ids[] = $attachment_id;
	}
	$out = do_shortcode('[gallery ids="' . implode(",", $ids) . '"]');
	return $out;
}

2) Put the gallery shortcode like this:
[repeating-images-gallery]

This is only a demo, you will need to customize the codes as what you want.

#410425

Hello

Thank you so much it works now but the the field slug must be my-slide not my-image.

Thnks Mr Luo Yang , you are awesome.

The forum ‘Types Community Support’ is closed to new topics and replies.