Skip Navigation

[Résolu] 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.

Ce fil est résolu. Voici une description du problème et la solution proposée.

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 support ticket is created Il y a 7 années et 9 mois. There's a good chance that you are reading advice that it now obsolete.
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.

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 réponses, has 2 voix.

Last updated by saidB1322 Il y a 7 années et 9 mois.

Assisted by: Luo Yang.

Auteur
Publications
#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

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

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.

Le forum ‘Types Community Support’ est fermé à de nouveaux sujets et réponses.

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