How to query details of images in gallery that is attached to a post
-
16-10-2019 - |
Question
I have several images attached to each post. I need to generate a gallery, but because it's heavily customized I don't want to use a gallery plugin, nor the gallery short code. It's also situated in a DIV that is totally separated from the post content/title etc. so it has to be stand-alone code in PHP, hard-coded.
Basically if there's a method to retrieve the list of attachments in a URL format.. and then pull in the proper sizes (filename_80X80.jpg or something along those lines - I know the filenames are manipulated after the thumbnail size).
I already have the thumbnail sizes covered using
add_image_size( 'ourwork_full', 589, 315, true );
add_image_size( 'ourwork_thumb', 80, 80, true );
in the funtions.php file of the template.
How can I achieve this? Do I need to use custom WP query? Or is there a function/template tag that I am missing?
Solution
Query for image attachments would be something like this (taken from Get The Image plugin/extension) with get_children()
:
$attachments = get_children( array(
'post_parent' => $args['post_id'],
'post_status' => 'inherit',
'post_type' => 'attachment',
'post_mime_type' => 'image',
'order' => 'ASC',
'orderby' => 'menu_order ID'
) );
Then you can loop through array and retrieve URLs with wp_get_attachment_image_src()
which will get you URL and dimensions.