2
votes

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?

2
Are you sure that you would want to loop over every image in the media library?ham-sandwich
Well, I would only retrieve the last 30 images, but first I need to know how to loop over the media libraryuser3250740

2 Answers

6
votes

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] . "'>";

endwhile;
2
votes

You can try this:

<section class="row align-middle">
        <?php 
        $query_images_args = array(
            'post_type'      => 'attachment',
            'post_mime_type' => 'image,video',// video files include
            'post_status'    => 'inherit',
            'orderby'        => 'post_date',
            'posts_per_page' =>  30,
        );

        $query_images = new WP_Query( $query_images_args );

        if($query_images->have_posts()) : 
            while($query_images->have_posts()) : 
                $query_images->the_post(); ?>

                <div class="small-6 medium-4 large-2 columns">
                    <?php echo $images = wp_get_attachment_image( $query_images->posts->ID, 'thumbnail' ); ?>
                </div>

            <?php endwhile; ?>
        <?php else : ?>
            <p>No media file yet</p>
        <?php endif;

        /* Restore original Post Data */
        wp_reset_postdata(); ?>
</section>

Remember restore original Post Data to avoid issues with others querys in the same page.