I’m using WPQuery
and get_children($query_images_args)
to query attachments from the current page. If the current page has no attachments assigned I’m querying the images of the parent page.
This works the way I want but there is one little extra I’m aiming for. I already asked a similar question (with the same code sample) here in the forum.
https://wordpress.stackexchange.com/questions/62624/only-get-attachments-that-are-associated-with-a-gallery
However in this question I ask for a different approach to address the same problem.
$query_images_args = array(
'post_type' => 'attachment',
'post_mime_type' =>'image',
'post_status' => 'inherit',
'posts_per_page' => -1,
'post_parent' => $post->ID
);
$attachments = get_children($query_images_args);
if ( empty($attachments) ) {
$query_images_args = array(
'post_type' => 'attachment',
'post_mime_type' =>'image',
'post_status' => 'inherit',
'posts_per_page' => -1,
'post_parent' => $post->post_parent
);
}
$query_images = new WP_Query( $query_images_args );
$images = array();
foreach ( $query_images->posts as $image) {
$images[] = wp_get_attachment_image_src( $image->ID, 'large');
}
I’m querying the attachment images of the current page and if there are no images assigned to the current page I query the images of the parent page. Moreover I’m doing an additional filter-thingy for only large images.
However I wonder if it is possible to query the images of the parent page if the current page has only “small” images.
So in essence, I’d like to get only “large” images of the current post if it has images and get only “large” images of the parent page if the current post has no images.
Any ideas on how to do that!
I’m not quite sure how you’re defining whether or not the current page has only “small” images. But I suspect the answer is right in the
wp_get_attachment_image_src
function, which returns not just the image url but also the width and height.So let’s say you were only interested in images that are more than 300 pixels wide. First, add this to your functions.php file:
Then, instead of your code above, you can add this: