Bizcochoman Bizcochoman - 1 year ago 75
PHP Question

How to loop over images in media library and display them? (wordpress)

I'm trying to make a simple gallery in wordpress (I've tried out some plugins but none of them convince me). The thing is that the solution I find more useful is to loop over the images I have already uploaded to the media library and display them (as a grid gallery).

The problem is that I don't find any information about how to loop over the media library images and display them as thumbnails, any suggestions?

Answer Source

Something like this?

$args = array(
        'post_type' => 'attachment',
        'post_mime_type' => 'image',
        'orderby' => 'post_date',
        'order' => 'desc',
        'posts_per_page' => '30',
        'post_status'    => 'inherit'

     $loop = new WP_Query( $args );

while ( $loop->have_posts() ) : $loop->the_post();

$image = wp_get_attachment_image_src( get_the_ID() ); 
echo "<img src='" . $image[0] . "'>";

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download