1

This question already has an answer here:

guys! I'm pretty sure this will be a piece of cake for y`all, but I'm trying to add pagination to my loop. I've tried some codes that were available around, and got to paginate it, but pages 2, 3, etc had the same content from page 1. What's the best solution for what I have at the moment?

<?php
    $recentPosts = new WP_Query();
    $recentPosts->query('showposts=5');
?>
<?php while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>
    <article>
    </article>
<?php endwhile; ?>

Thanks in advance.

marked as duplicate by fuxia Sep 26 '15 at 5:40

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

4

Likely this is happening because you are using a custom page template. Try the following. I've commented the steps along the way. Hope it helps.

<?php 
  //get the current page
  $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

  //pagination fixes prior to loop
  $temp =  $query;
  $query = null;

  //custom loop using WP_Query
  $query = new WP_Query( array( 
    'post_status' => 'publish',
    'orderby' => 'date',
    'order' => 'ASC' 
  ) ); 

 //set our query's pagination to $paged
 $query -> query('post_type=post&posts_per_page=5'.'&paged='.$paged);

 if ( $query->have_posts() ) : 
   while ( $query->have_posts() ) : $query->the_post();
    ?>
      <li>
        <?php if ( has_post_thumbnail()) : ?>
          <?php the_post_thumbnail();?>
        <?php endif; ?>
        <div class="someclass" >
            <h2><?php the_title(); ?></h2> 
            <?php the_content(); ?>
        </div> 
      </li>
  <?php endwhile;?>

  <?php //pass in the max_num_pages, which is total pages ?>
  <div class="pagenav">
    <div class="alignleft"><?php previous_posts_link('Previous', $query->max_num_pages) ?></div>
    <div class="alignright"><?php next_posts_link('Next', $query->max_num_pages) ?></div>
  </div>

<?php endif; ?>

<?php //reset the following that was set above prior to loop
$query = null; $query = $temp; ?>
  • 1
    For some reason it's still returning to all pages the content from page 1. It's such a bizarre behavior. – Paulo Gabriel Sep 26 '15 at 0:20
  • What template are you using for this loop? – amespower Sep 26 '15 at 1:18
  • Not a default one... I created a new template page called gallery to return only the featured image and permalinks. – Paulo Gabriel Sep 26 '15 at 15:03
0

There are few ways to do pagination. Here is one:

<?php $query = new WP_Query( array( 'posts_per_page' => 5 ) ); ?>

<?php while ($query->have_posts()) : $query->the_post(); ?>
   <article>
      <h2><a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
   <article>
<?php endwhile; ?>

<div class="nav-previous alignleft"><?php next_posts_link( 'Older posts' ); ?></div>
<div class="nav-next alignright"><?php previous_posts_link( 'Newer posts' ); ?></div>

Of course it won't work if your WP_Query is not built correctly.

  • Note that "showposts" has been deprecated since WP 2.1 (i.e., ancient times). – CK MacLeod Sep 25 '15 at 21:10
  • I've updated to your code and it still doesn't work. :/ – Paulo Gabriel Sep 25 '15 at 22:09
  • Did you read the codex page re pagination? Specifically this part: codex.wordpress.org/… – shanebp Sep 25 '15 at 22:39
0

Here my post type is 'news_events', the pagination will be displayed after every 4 posts title is displayed.

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$news_events = array( 'post_type' => 'news_events', 'posts_per_page' => 4, 'paged' => $paged);
$wp_query = new WP_Query( $news_events );
if ( $wp_query->have_posts() ) : ?>
    <?php while ( $wp_query->have_posts() ) : $wp_query->the_post();?>
          <?php the_title(); echo "<br/>"; ?>
    <?php endwhile; ?>
    <nav>
      <?php previous_posts_link('&laquo; Newer',$wp_query->max_num_pages); ?>
      <?php next_posts_link('Older &raquo;',$wp_query->max_num_pages); ?>
    </nav>
    <?php wp_reset_postdata(); ?>                           
    <?php else : ?>
        <p><?php _e( 'Sorry, no news events posts at this time.', 'theme' ); ?></p>
<?php endif; ?>
  • Please add an explanation to this code snippet. – fuxia Sep 26 '15 at 5:39

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