Skip Navigation

[Resolved] Get ID of Repeater Image Field insted of URL

This support ticket is created 8 years, 2 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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

This topic contains 10 replies, has 2 voices.

Last updated by Minesh 8 years, 1 month ago.

Assisted by: Minesh.

Author
Posts
#365969

I want to take the ID's from a list of repeater images and put them into wordpress standard gallery shortcode but in raw output format I get the link of each image in the repeater image field that I want.

How can I take the ID instead of the link for repeater image field?

I have tried that sollution (https://toolset.com/forums/topic/how-to-get-ids-of-repeating-images/) which does not work and this one too (https://toolset.com/forums/topic/get-id-of-attachment/) which only work if the image field is not repeater!

thanks

#366078

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

As you can see that gallery shortcode works as given under:

[gallery ids="729,732,731,720"]

Where "ids" attribute have comma(,) separated IDs of images.

Could you please try following code - add the following code to your theme's functions.php file:

function prefix_get_img_ids($atts) {

 $post_id= $atts["post_id"];
 $images = (array) get_post_meta($post_id, 'wpcf-rep-field', false); // cast to array in case there is only one item
 $ids = array();
 
 global $wpdb;
  
 foreach($images as $img) {
  $query = "SELECT ID FROM {$wpdb->posts} WHERE guid='$img'";
  $id = $wpdb->get_var($query);
  $ids[] = $id;
 }
 
 return implode(",",$ids);
}
add_shortcode("get-image-ids", "prefix_get_img_ids");
[get-image-ids post_id="13"]

I hope this will fix your issue. Please let me know your feedback.

#366116

I have included your code in my functions.php file and I include the shortcode in the template I have created but nothing happens!

Can you please explain me a little better what else should i do please in order to make that work?

#366137

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Could you please let me know your image field name as well as the post ID from which you would like to fetch the images.

#366139

My image field name is gallery-images. The code will be in a views template, so it will has to work for every post, so I cannot have a static post ID!

#366148

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

The code will be in a views template, so it will has to work for every post, so I cannot have a static post ID!
=> Are you using views to display post? which content template you are using.
=> Could you please tell me where exactly you would like to display gallery images?

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I would additionally need your permission to de- and re-activate Plugins and the Theme, and to change configurations on the site. This is also a reason the backup is really important. If you agree to this, please use the form fields I have enabled below to provide temporary access details (wp-admin and FTP).

I have set the next reply to private which means only you and I have access to it.

#366167

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Ok. I'm able to locate your content template. Just to know where exactly you want to display the default gallery.

You want to dispaly gallery at follwoing accordion? like this: [gallery ids="729,732,731,720"]
[accordion_item title="Gallery"]

So this should be like this:
[accordion_item title="Gallery"]
[gallery ids="729,732,731,720"]
[/accordion_item]

#366168

Yeah exactly!

#366208

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Could you please check now.

I've adjusted the code in your functions.php file as follows.

function prefix_get_img_ids($atts) {
global $post;

 $images = (array) get_post_meta($post->ID, 'wpcf-gallery-images', false); // cast to array in case there is only one item
 $ids = array();
  
 global $wpdb;
   
 foreach($images as $img) {
  $query = "SELECT ID FROM {$wpdb->posts} WHERE guid='$img'";
  $id = $wpdb->get_var($query);
  $ids[] = $id;
 }
  
 return implode(",",$ids);
}
add_shortcode("get-image-ids", "prefix_get_img_ids");

In your content template - I've added code as follows:

[accordion_item title="Gallery"]
[gallery ids="[get-image-ids]"]
[/accordion_item]
#373811
NHi2LF9.png

The gallery images have big space between them and they are in columns of 3. How can I make that have smaller margin and be as more columns as the width allows?

#373920

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

You need to change your CSS to style your gallery as you want. Please refer to the following link:
hidden link

To change number of columns to display, please refer to section "columns " and for image size refer to section "size":
https://codex.wordpress.org/Gallery_Shortcode

If you still need further assistance, for your any custom programming need, please feel free to contact our certified partners:
https://toolset.com/consultant/

Please open a new ticket for your each new question. This will help other users with similar problems to find solutions when searching the forum.

Thank you for your understanding.

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