-1

I've copied the code from the Wordpress documentation on adding pagination to a custom query using 'paged' => $paged. However, on my page it's not working. What am i doing wrong??

  <?php 
      $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
      $args = array('post_type' => 'post', 'posts_per_page' => 10, 'paged' => $paged); 
      $the_query = new WP_query($args); 
      if ( $the_query->have_posts() ) : ?> 
        <ul class="pis-ul">
      <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
           <li class="pis-li">
                 <a class="pis-title-link" href="<?php echo the_permalink(); ?>">
                 <div class="news-thumbnail">

                 <?php

                    if(has_post_thumbnail()) {
                     echo the_post_thumbnail('news-thumbnail');  
                    }

                    ?>
                    </div>
                    <p class="pis-title">
                        <?php the_title(); ?>       
                    </p>
                    <p class="pis-excerpt">
                        <?php the_excerpt(); ?>
                    </p>
                    <p class="pis-utlility">
                        <span class="pis-date">Published on <?php echo the_date(); ?></span>
                    </p>
                 </a>     
            </li>
      <?php endwhile; ?>
        <ul>
      <div class="nav-previous alignleft"><?php next_posts_link( 'Older posts' ); ?></div>
      <div class="nav-next alignright"><?php previous_posts_link( 'Newer posts' ); ?></div>

      <?php else:  ?>
        <p>
          <?php _e('Sorry, this page does not exist.'); ?>
        </p>
        <?php endif; ?> 
  • "not working" is vague. What is/isn't happening, and what do you expect/want to happen? – janh Jan 19 '18 at 17:23
  • Not working meaning the next and previous buttons aren't showing up. I want them to display so i can go to the next page of posts. – Shaun Jan 19 '18 at 18:45
3

next_post_link will use the current query (which is the global $wp_query in your case). If you want to use them with other queries, pass them the maximum page number as the second argument. There's an example in the codex.

For previous_post_link it doesn't matter, because it'll only show a link if you're on a page other than the first - and if you are, there has to be a previous page. The link will show up when the paged parameter is > 1.

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.