A similar question has been asked before, but it doesn't work for me at all: How to paginate a custom WP_query() loop - Wordpress
I have a loop with my media files which works fine, but I can't seem to figure out how to paginate it correctly. It's basically a loop that outputs my images in a grid.
So far, I've managed to echo a "Load older entries", but when I click on it to go to the second page, it shows a 404 at localhost/myweb/page/2/
This is my code currently:
$paged = ( get_query_var('page') ) ? get_query_var('page') : 1;
$query_args = array(
'post_type' => 'attachment',
'post_mime_type' => 'image',
'orderby' => 'rand',
'order' => 'desc',
'post_status' => 'inherit',
'posts_per_page' => 60,
'paged' => $paged
);
$the_query = new WP_Query( $query_args );
?>
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); // run the loop ?>
<?php
$image = wp_get_attachment_image_src( get_the_ID(), 'large' );
$things = '<img src="' . $image[0] . '">';
$imgurl = wp_get_attachment_link( $attachment->ID, '' , true, false, $things );
echo '<li>' . $imgurl . '</li>';?>
<?php endwhile; ?>
</ul>
<?php if ($the_query->max_num_pages > 1) { // check if the max number of pages is greater than 1 ?>
<nav class="prev-next-posts">
<div class="prev-posts-link">
<?php echo get_next_posts_link( 'Older entries', $the_query->max_num_pages ); // display older posts link ?>
</div>
<div class="next-posts-link">
<?php echo get_previous_posts_link( 'Newer Entries' );?>
</div>
</nav>
<?php } ?>