Skip Navigation

[Resolved] I’d like to make a Gallery from images added in a Post Type

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

Problem:
Make a Gallery of images from a Post Type
Solution:

Make use of the default wordpress gallery shortcode with Toolset image ID's

The solution for this can be seen below.
https://toolset.com/forums/topic/id-like-to-make-a-gallery-from-images-added-in-a-post-type/#post-1202018

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

Last updated by marcelO-2 5 years, 9 months ago.

Assisted by: Shane.

Author
Posts
#1201731

Tell us what you are trying to do?
Make a Gallery of images from a Post Type
Is there any documentation that you are following?
<h3 style="text-align: center;"><span style="text-decoration: underline;">Performer Photo Gallery:</span></h3>
<p>[gallery ids="[wpv-for-each field="wpcf-promo-image-gallery"] {!{types field='promo-image-gallery' title='%%TITLE%%' alt='%%ALT%%' size='medium' resize='proportional' separator=', ' item='$parent'}!}{!{/types}!}[/wpv-for-each]"]</p>

https://toolset.com/2017/10/adding-a-gallery-of-images-for-custom-posts/
Is there a similar example that we can see?

What is the link to your site?
hidden link

#1201871

Shane
Supporter

Languages: English (English )

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

Hi Marcel,

Thank you for contacting our support forum.

I dont see how your setup will work since you are using a repeatable image field . I suspect that the gallery shortcode needs the ID of the post.

What you can do is to try this solution below
https://toolset.com/forums/topic/get-the-image-id-of-the-repeatable-image-fields/#post-1201811

So you will pass this shortcode into the gallery shortcode to pass the ids.

If this does not work please let me know.

Thanks,
Shane

#1202000

I added the code to my functions.php

I then tried to use the code like this:

<h3 style="text-align: center;"><span style="text-decoration: underline;">Performer Photo Gallery:</span></h3>
[gallery size="medium" ids="[get_image_id id='[wpv-post-id]' field='wpcf-promo-image-gallery']"]

This does not work.

#1202018

Shane
Supporter

Languages: English (English )

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

Hi Marcel,

This is the default gallery shortcode for wordpress correct?

Try this new shortcode.

    // Add Shortcode
function get_image_id( $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]);
             }
        }
         
 $final_list = implode(",", $id_list); 
 $out = do_shortcode('[gallery size="medium" ids="'.$final_list.'"] ');
return $out; 
}
add_shortcode( 'get_image_id', 'get_image_id' );

This should hopefully resolve the issue. I told it to just output the gallery shortcode.

Please let me know if this helps.
Thanks,
Shane

#1202155

Okay, let me see if I can work out how to use this shortcode:

- It must always show a Gallery based on the current Post ID

I've modified the shortcode output to this:

$out = do_shortcode('[TS_VCSC_Lightbox_Gallery content_images_altdata="false" content_style="Freewall" fullwidth="true" breakouts="4" lightbox_effect="fade" lightbox_backlight="hideit" lightbox_save="true" ids="'.$final_list.'"][/TS_VCSC_Lightbox_Gallery] ');

What is the usage of this shortcode? Is it [get_image_id id="[wpv-post-id]" field="wpcf-promo-image-gallery"]?

If I use the above shortcode, this is the output: -> " field="wpcf-promo-image-gallery"]

As I understand, .$final_list needs to be list of IDs of images attached to the Post ID (the current Profile)

#1202157

Here's what the shortcode SHOULD have:

$image_urls = get_post_meta(get_the_ID(),$atts['field']);
#1202160

Herewith the final working code:

     // Add Shortcode
function get_perf_gallery( $atts ) {
 global $wpdb;
  
    // Attributes
    $atts = shortcode_atts(
        array(
            'field' => '',
            'id' => ''
        ),
        $atts
    );
  
    $image_urls = get_post_meta(get_the_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]);
             }
        }
          
 $final_list = implode(",", $id_list); 
 $out = do_shortcode('[TS_VCSC_Lightbox_Gallery content_images_altdata="false" content_style="Freewall" fullwidth="true" breakouts="4" lightbox_effect="fade" lightbox_backlight="hideit" lightbox_save="true" content_images="'.$final_list.'"][/TS_VCSC_Lightbox_Gallery] ');
return $out; 
}
add_shortcode( 'get_perf_gallery', 'get_perf_gallery' );