Site icon Hip-Hop Website Design and Development

How to get all images in Media Gallery with pagination?

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

            ?>