I've been working on a wordpress site, and I'm trying to build a page where visitors can see all of the images that have been uploaded to the wordpress media gallery, regardless of whether or not said images are attached to any particular posts. The images from the query are showing up, but the pagination links are not. I can view the paginated pages by including page/2/ or page/3/ at the end of my URL, but the pagination links to bring viewers to those pages aren't showing up at all.

The gallery is located at http://www.nickpassaro.com/clientsitedev/NJRI/gallery/ The images on http://www.nickpassaro.com/clientsitedev/NJRI/gallery/page/2/ and http://www.nickpassaro.com/clientsitedev/NJRI/gallery/page/3/ are not the same images as the images on http://www.nickpassaro.com/clientsitedev/NJRI/gallery/ so I assume pagination is possible here.

The code for the gallery template is as follows:

Template Name: Gallery
get_header(); ?>
  <div class="main" role="main">
    <div class="standard-section">
      <div class="gallery">
        <h1 class="big-centered-h1">Photo Category #1</h1>
        <div class="gallery-images">
            $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
            $gallery_page_args = array(
              'post_type' => 'attachment',
              'posts_per_page' => 8,
              'paged' => $paged,
              'post_mime_type' =>'image'
            $gallery_page_query = get_posts($gallery_page_args);
            if ($gallery_page_query) {
              foreach ($gallery_page_query as $attachment) {
                echo '<a class="img-lightbox-wrapper" href="';
                echo wp_get_attachment_url($attachment->ID);
                echo '">';
                echo '<img class="gallery-image" src="';
                echo wp_get_attachment_thumb_url($attachment->ID);
                echo '">';
                echo '</a>';
              $gallery_page_pagination_args = array(
                'total' => $gallery_page_query->max_num_pages,
                'prev_text' => 'Go Back',
                'next_text' => 'See More'
              echo '<div class="clearfix"></div>';
              echo paginate_links($gallery_page_pagination_args);
              echo '<div class="clearfix"></div>';
<?php get_footer(); ?>

Any help would be greatly appreciated!

1 Answer 1


previous_posts_link and next_posts_link are for archive page pagination, they both check if is_single is not true, so will not work on any sort of single post, page, or custom post type. use paginate_links instead or just manually build your pagination links.

  • I added paginate links, but it still isn't working. I added it in place of: echo '<div class="clearfix"></div>'; echo '<div class="left-pagination">'; previous_posts_link('Go back'); echo '</div>'; echo '<div class="right-pagination">'; next_posts_link('See More', $the_gallery_page_attachments->max_num_pages); echo '</div>'; echo '<div class="clearfix"></div>'; I set the 'total' to be $the_gallery_page_attachments->max_num_pages, I set the prev_text to be 'Go Back', and I set the next_text to be 'See More' Feb 27, 2015 at 16:42
  • edit your question and update your code with what you're currently using. also if you search this site for paginate_links, you'll find some working examples in other answers.
    – Milo
    Feb 27, 2015 at 17:32
  • I've updated the code in my question. The pagination links are still not showing up. Did I set this up wrong? I've put my pagination in the nesting structure so that the pagination happens if there are posts, after the query does all of the posts Feb 27, 2015 at 19:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.