query - How to get all images in Media Gallery with pagination?

admin2025-01-07  4

I want to get all images in the image gallery with pagination so that I can use infinite-scroll with it.

The infinite scroll works but it's taking the posts' number of post, not the images. That means that if I have 6 posts, and the "Blog pages show at most" is 2, the number of pages will be 3, even if I have more images to load.

I'm also using FoundationPress as a framework if that helps.

here's my code:

<?php

            $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
            $args = array(
                'posts_per_page' => 10,
                'post_type' => 'attachment',
                'paged'     => $paged
            );

            $attachments = get_posts( $args );
            if ( $attachments ) {
                foreach ( $attachments as $attachment ) {
                    $ia = wp_get_attachment_image_src( $attachment->ID, 'full' );
                    ?>
                    <div class="grid__item">
                        <figure width="<?php echo $ia[1]; ?>" height="<?php echo $ia[2]; ?>">
                            <?php echo wp_get_attachment_image( $attachment->ID, 'full' ); ?>
                        </figure>
                    </div>
                    <?php 
                }
            }

            ?>

I want to get all images in the image gallery with pagination so that I can use infinite-scroll with it.

The infinite scroll works but it's taking the posts' number of post, not the images. That means that if I have 6 posts, and the "Blog pages show at most" is 2, the number of pages will be 3, even if I have more images to load.

I'm also using FoundationPress as a framework if that helps.

here's my code:

<?php

            $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
            $args = array(
                'posts_per_page' => 10,
                'post_type' => 'attachment',
                'paged'     => $paged
            );

            $attachments = get_posts( $args );
            if ( $attachments ) {
                foreach ( $attachments as $attachment ) {
                    $ia = wp_get_attachment_image_src( $attachment->ID, 'full' );
                    ?>
                    <div class="grid__item">
                        <figure width="<?php echo $ia[1]; ?>" height="<?php echo $ia[2]; ?>">
                            <?php echo wp_get_attachment_image( $attachment->ID, 'full' ); ?>
                        </figure>
                    </div>
                    <?php 
                }
            }

            ?>
Share Improve this question asked Oct 27, 2017 at 10:01 mark-in-motionmark-in-motion 1592 silver badges15 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

EDIT: I didn't read your code properly, sorry!

You're using get posts which will only retrieve posts. To get all the images from the media library you'll need to use WP_query.

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

$query_images = new WP_Query( $query_images_args );
$images = array();
foreach ( $query_images->posts as $image) {
    $images[]= wp_get_attachment_url( $image->ID );
}

This code will put all the URLs of images from the media library into the $images array.

ref: https://wpeden.com/tipsntuts/list-all-images-at-media-gallery-in-your-wordpress-site/

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736260823a693.html

最新回复(0)