I have a custom post type that has a custom field for a gallery images. I was trying to use some of your previous documenation to output list of image IDs that I can place in a shortcode for another plugin to use.
I tried these https://toolset.com/forums/topic/add-ids-of-repeating-images-to-gallery/ and https://toolset.com/forums/topic/id-like-to-make-a-gallery-from-images-added-in-a-post-type/ without success
custom field is slugged as gallery-image and I was trying to use this shortcode in the post for displaying.
[get_image_id postid="[wpv-post-id]" fieldslug="gallery-image"] - this only returned one ID, and there are 5 plus images in most of the posts
[get_image_ids_repeating post_id="[wpv-post-id]" field="gallery-image"] - nothig is returned
Shane
Supporter
Languages:
English (English )
Timezone:
America/Jamaica (GMT-05:00)
Hi Larry,
Thank you for getting in touch.
Assuming that your field is a Types field then you will need to do it like this.
[get_image_id postid="[wpv-post-id]" fieldslug="wpcf-gallery-image"]
Adding the wpcf- prefix to the field slug.
Please let me know if this helps.
Thanks,
Shane
Shane this only produces the IP for the post I believe and not each of the gallery images. This code only brings back one ID "350",
[get_image_id postid=”350″ fieldslug=”gallery-image”]
[get_image_id postid=”350″ fieldslug=”wpcf-gallery-image”]
Here's a sample page link
hidden link
I want it to bring back all of these IDs.ids="362,361,360,359,358,357,356" for example
Shane
Supporter
Languages:
English (English )
Timezone:
America/Jamaica (GMT-05:00)
Hi Larry,
Did you add the corresponding code to the Toolset custom code section in Toolset -> Settings -> Custom Code?
If you didn't please add the following code there and ensure you click on the activate button once you've added it.
// 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]);
}
}
return implode(",", $id_list);
}
add_shortcode( 'get_image_id', 'get_image_id' );
Currently your shortcode isn't rendering on the frontend.
Thanks,
Shane
Yes I have this code in Code Snippets.
I'm still only getting this returned
[get_image_ids_repeating post_id=”350″ field=”gallery-image”]
Shane
Supporter
Languages:
English (English )
Timezone:
America/Jamaica (GMT-05:00)
Hi Larry,
The shortcode name that you are using is incorrect.
The correct format is.
[get_image_id postid="[wpv-post-id]" fieldslug="wpcf-gallery-image"]
What you have now is.
[get_image_ids_repeating post_id="350″ field="gallery-image"]
Please change it to the correct format and let me know if it starts working.
Thanks,
Shane
Sorry, Shane. I appreciate the help, but this isn't getting me where I need to be. I want to generate these IDs to convert this:
[gallery columns="6" ess_grid_gal="project-thumnail-slider" ids="362,361,360,359,358,357,356" orderby="rand"] (where these IDs are coming from the images my client uploaded to the gallery field as shown in the screen shot.
to:
[gallery columns="6" ess_grid_gal="project-thumnail-slider" ids="[get_image_id postid="[wpv-post-id]" fieldslug="wpcf-gallery-image"]" orderby="rand"]
Shane
Supporter
Languages:
English (English )
Timezone:
America/Jamaica (GMT-05:00)
Hi Larry,
Please allow me to have admin access to the website so that I can check on this for you.
I believe the issue here is that the wordpress Gallery shortcode doesn't allow a nested shortcode so you will need to modify the shortcode itself.
Let me know a sample page where I can test this out.
Thanks,
Shane
Shane
Supporter
Languages:
English (English )
Timezone:
America/Jamaica (GMT-05:00)
Hi Larry,
The shortcode should now be working.
I had to wrap the gallery shortcode inside the custom shortcode as well.
// 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 = attachment_url_to_postid($image_url);
array_push($id_list, $attachment);
}
}
$list_of_id = implode(",", $id_list);
return do_shortcode("[gallery columns='6' ess_grid_gal='project-thumnail-slider' ids='".$list_of_id."' orderby='rand']");
}
add_shortcode( 'get_image_id', 'get_image_id' );
So the final code to run this would be [get_image_id id="[wpv-post-id]" field="wpcf-gallery-image"]
Please let me know if this helps.
Thanks,
Shane
My issue is resolved now. Thank you!