Problem: I would like to generate a list of image ids from a repeating image field.
Solution: Use the following custom shortcode and utility function.
function get_attachment_id( $url ) { $attachment_id = 0; $dir = wp_upload_dir(); if ( false !== strpos( $url, $dir['baseurl'] . '/' ) ) { // Is URL in uploads directory? $file = basename( $url ); $query_args = array( 'post_type' => 'attachment', 'post_status' => 'inherit', 'fields' => 'ids', 'meta_query' => array( array( 'value' => $file, 'compare' => 'LIKE', 'key' => '_wp_attachment_metadata', ), ) ); $query = new WP_Query( $query_args ); if ( $query->have_posts() ) { foreach ( $query->posts as $post_id ) { $meta = wp_get_attachment_metadata( $post_id ); $original_file = basename( $meta['file'] ); $cropped_image_files = wp_list_pluck( $meta['sizes'], 'file' ); if ( $original_file === $file || in_array( $file, $cropped_image_files ) ) { $attachment_id = $post_id; break; } } } } return $attachment_id; } add_shortcode('media-info-multi', 'media_info_multi_func'); function media_info_multi_func($atts, $content){ global $wpdb; $atts = shortcode_atts( array( 'slug' => 'photos', 'info' => 'title', ), $atts); $res = ''; $urls = types_render_field($atts['slug'], array('output' => 'raw', 'separator'=> ',')); $arr = explode(',', $urls); $attachment_id = array(); foreach($arr as $k => $v){ $attachment_id[] = get_attachment_id( $v ); } $res = implode(',', $attachment_id); return $res; }
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 |
---|---|---|---|---|---|---|
8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | - | - |
13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | - | - |
Supporter timezone: America/New_York (GMT-04:00)
This topic contains 6 replies, has 2 voices.
Last updated by 6 years, 7 months ago.
Assisted by: Christian Cox.