Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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. 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!

share|improve this question

1 Answer 1

up vote 4 down vote accepted
+150

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:

function return_my_big_images( $id ) {

    $query_images_args = array(
        'post_type' => 'attachment',
        'post_mime_type' => 'image',
        'post_status' => 'inherit',
        'posts_per_page' => -1,
        'post_parent' => $id
    );

    $attachments = get_children( $query_images_args );

    $big_images = array();
    foreach( $attachments as $image ) {
        $image_src = wp_get_attachment_image_src( $image->ID, 'full' );
        if ( $image_src[1] > 300 ) // $image_src[1] is where pixel width is stored
            $big_images[] = $image_src[0]; // $image_src[0] is its url
    }

    return $big_images;

}

Then, instead of your code above, you can add this:

global $post;

// return all images bigger than 300px wide attached to current post
$big_images = return_my_big_images( $post->ID );

// if none, return images bigger than 300px wide attached to post parent
if ( empty( $big_images ) )
    $big_images = return_my_big_images( $post->post_parent );
share|improve this answer
    
thank you very much! –  mathiregister Aug 27 '12 at 7:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.