Forums

[resolved] how to filter images by its attributes? (5 posts)

  1. t.g
    Member
    Posted 1 year ago #

    for example i only want to show attached images which are 960px by 200px.

  2. TM3909
    Member
    Posted 1 year ago #

    $args = array(
        'post_type' => 'attachment',
        'numberposts' => -1,
        'post_status' => null,
        'post_parent' => $main_post_id
    );
    
    $attachments = get_posts($args);
    
    foreach ($attachments as $attachment)
    {
        the_attachment_link($attachment->ID, false);
    }

    That gets the image. Then check the size with an if statement asking:
    filesize( get_attached_file( $attachment->ID ) );

  3. t.g
    Member
    Posted 1 year ago #

    can you place an if statement within a foreach loop? or where do you mean shall i put the if statement?

  4. TM3909
    Member
    Posted 1 year ago #

    $filesize = filesize( get_attached_file( $attachment->ID ) );
    if( $filesize == WHATYOUWANTTHEFILESIZETOBE){
    $args = array(
        'post_type' => 'attachment',
        'numberposts' => -1,
        'post_status' => null,
        'post_parent' => $main_post_id
    );
    
    $attachments = get_posts($args);
    
    foreach ($attachments as $attachment)
    {
        the_attachment_link($attachment->ID, false);
    } 
    
    else  { echo 'No images for you!'; }

    Not tested, but it gives you the idea.

  5. t.g
    Member
    Posted 1 year ago #

    thanks for your help!

    i have a solution too, finally!

    <?php
    
    $attachments = get_children( 
    
    	array(
    		'post_parent' => get_the_ID(),
    		'post_type' => 'attachment',
    		'post_mime_type' => 'image'
    		) );
    
    	foreach ( $attachments as $attachment_id => $attachment ) {
    		$src = wp_get_attachment_image_src( $attachment_id, 'full' );
    
    		if ($src[2] == 500){
    			echo wp_get_attachment_image($attachment->ID, 'full');
    		}
    }
    
    ?>

Topic Closed

This topic has been closed to new replies.

About this Topic