Forums

[resolved] Filter attached images (2 posts)

  1. t.g
    Member
    Posted 1 year ago #

    hello!

    i am using the get_posts method to display the images of my media library as seen below

    <?php
    $args = array(
    	'order'          => 'ASC',
    	'post_type'      => 'attachment',
    	'post_mime_type' => 'image',
    	'post_status'    => null,
    	'numberposts'    => -1,
    );
    
    $attachments = get_posts($args);
    
    if ($attachments) {
    	foreach ($attachments as $attachment) {
    		echo wp_get_attachment_image($attachment->ID, 'full', false);
    	}
    }
    ?>

    in addition to the array i want to apply a filter to show only images with a fullsize-height of 500px.

    i tried to do it with someting like the code below, but it doesnt seem to work..

    $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' );
    var_dump($src);
    }

    thanks in advance for your help

    ps: i dont want to show all images with a height of 500px, i just want to show the ones which already have a fullsize-height in the media library of 500px.

  2. t.g
    Member
    Posted 1 year ago #

    just like that =)

    <?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