Skip Navigation

[Resolved] trying to make a gallery from repeating image field.

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

Last updated by staceyz 7 years, 5 months ago.

Assisted by: Luo Yang.

Author
Posts
#537413

I found this thread:
https://toolset.com/forums/topic/create-wp-gallery-from-multiple-image-field/

and am trying to do the same thing (Create gallery out of a repeating image field), but cannot get it to work.

I have a repeating image field named "Image Gallery" with the slug 'partner-image-gallery'

I placed this in functions.php

add_shortcode( 'gallery_from_image_fields', function( $atts ){
 
    global $wpdb;
 
    $postid = $atts['postid'];
    $slug = 'wpcf-partner-image-gallery' . $atts['slug'];
 
 
    // Retrieve image field urls
    $images = (array) get_post_meta( $postid, $slug, false);
 
    $ids = array();
    // Convert the urls to post ids for the images
    foreach ($images as $image) {
 
        $attachment = $wpdb->get_col( $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE guid='%s';", $image ) ); 
        $ids[] = $attachment[0];
    }
 
    $ids_list = join( ',', $ids );
 
    $gallery = '[gallery ids="' . $ids_list . '"]';
 
    $out = do_shortcode( $gallery );
 
    return $out;
});

and then this in my page template

[gallery_from_image_fields postid='[wpv-post-id]' slug='partner-image-gallery']

But I can't get it to work. Any idea what I may be doing wrong?

Thanks so much!!

#537505

Dear staceyz,

I assume the custom image filed "partner-image-gallery" is created with Types plugin, Types will pre-pend "wpcf-" before the field slug, please try this, edit this line from:

    $slug = 'wpcf-partner-image-gallery' . $atts['slug'];

To:

    $slug = 'wpcf-' . $atts['slug'];

And test again

#537758

Oh - perfect. Thanks for the help and quick reply!