Title: WP Query Pagination
Last modified: August 20, 2016

---

# WP Query Pagination

 *  [OlyReeve](https://wordpress.org/support/users/olyreeve/)
 * (@olyreeve)
 * [14 years ago](https://wordpress.org/support/topic/wp-query-pagination/)
 * Hi all,
 * I’m a php beginner and I’m having some problems with pagination. Can anyone help?
 *     ```
       <?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
       	<?php global $post;
       		$args = array( 'posts_per_page' => 2, 'child_of' => 6, 'offset' => 1);
       		$myposts = get_posts( $args );
       		$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
       		foreach( $myposts as $post ) :	setup_postdata($post); ?>
       			<div class="articleOld">
       				<a href="<?php the_permalink(); ?>"><?php if ( has_post_thumbnail() ) { the_post_thumbnail('medium'); } ?></a>
       				<h4><?php the_time('jS F Y') ?> in <?php the_category(', '); ?></h4>
       				<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
       				<?php the_excerpt(); ?>
       			</div>
   
       	<?php endforeach; ?>
       <?php endwhile; ?>
       	<?php posts_nav_link(' — ', __('&laquo; Newer Posts'), __('Older Posts &raquo;')); ?>
       ```
   

Viewing 5 replies - 1 through 5 (of 5 total)

 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [14 years ago](https://wordpress.org/support/topic/wp-query-pagination/#post-2947701)
 * You are using a get_posts() call inside the Loop. You should modify the Loop 
   instead (UNTESTED):
 *     ```
       <?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
          $args = array( 'posts_per_page' => 2, 'child_of' => 6, 'offset' => 1, 'paged' => $paged);
          query_posts($args);
          if (have_posts()) : while (have_posts()) : the_post(); ?>
             <div class="articleOld">
                <a href="<?php the_permalink(); ?>"><?php if ( has_post_thumbnail() ) { the_post_thumbnail('medium'); } ?></a>
                <h4><?php the_time('jS F Y') ?> in <?php the_category(', '); ?></h4>
                <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
                <?php the_excerpt(); ?>
             </div>
       <?php endwhile; endif; ?>
       <?php posts_nav_link(' — ', __('« Newer Posts'), __('Older Posts »')); ?>
       ```
   
 *  Thread Starter [OlyReeve](https://wordpress.org/support/users/olyreeve/)
 * (@olyreeve)
 * [14 years ago](https://wordpress.org/support/topic/wp-query-pagination/#post-2947738)
 * Thanks for your help, it’s still not quite working right, here’s my full code–
   it’s messy 🙂
 *     ```
       <?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
       	<?php global $post;
       		$args = array( 'numberposts' => 1, 'child_of' => 6);
       		$myposts = get_posts( $args );
       		foreach( $myposts as $post ) :	setup_postdata($post); ?>
       			<div class="article">
       				<h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
       				<h5><?php the_category(', '); ?></h5>
       				<h4>Written <?php the_time('jS F Y') ?> by <?php the_author_posts_link(); ?></h4>
       				<a href="<?php the_permalink(); ?>"><?php if ( has_post_thumbnail() ) { the_post_thumbnail('large'); } ?></a>
       				<?php the_excerpt(); ?>
       			</div>
       	<?php endforeach; ?>
       <?php endwhile; ?>
       <?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
          $args = array( 'posts_per_page' => 2, 'child_of' => 6, 'offset' => 1, 'paged' => $paged);
          query_posts($args);
          if (have_posts()) : while (have_posts()) : the_post(); ?>
             <div class="articleOld">
                <a href="<?php the_permalink(); ?>"><?php if ( has_post_thumbnail() ) { the_post_thumbnail('medium'); } ?></a>
                <h4><?php the_time('jS F Y') ?> in <?php the_category(', '); ?></h4>
                <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
                <?php the_excerpt(); ?>
             </div>
       <?php endwhile; endif; ?>
       <?php posts_nav_link(' — ', __('« Newer Posts'), __('Older Posts »')); ?>
       ```
   
 * and a link if you need to see whats going on, thanks!
    [http://kitehub.co.uk/bude/beaches](http://kitehub.co.uk/bude/beaches)
 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [14 years ago](https://wordpress.org/support/topic/wp-query-pagination/#post-2947750)
 * What is the purpose of the first `if (have_posts())...` loop? It has the get_posts()
   call inside it and that is more than likely not what you want.
 * Which part of the page are you wanting to paginate?
 *  Thread Starter [OlyReeve](https://wordpress.org/support/users/olyreeve/)
 * (@olyreeve)
 * [14 years ago](https://wordpress.org/support/topic/wp-query-pagination/#post-2947751)
 * Thanks for your patience. I just want the first post to appear differently to
   rest of the posts. I created two loops because I didn’t know how to make the 
   first one different. This is probably the reason it’s messing up! Cheers!
 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [14 years ago](https://wordpress.org/support/topic/wp-query-pagination/#post-2947753)
 * OK. Just use a single loop and a counter to do something different for the first
   post.
 * But that brings up another question: Do you want to do something different only
   on the first page, or every page? Since the answer is usually ‘Only on the first
   page.’, that is how the sample code below should work.
 *     ```
       <?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
          $args = array( 'posts_per_page' => 2, 'child_of' => 6, 'offset' => 1, 'paged' => $paged);
          query_posts($args);
          $counter = 0;
          if (have_posts()) : while (have_posts()) : the_post();
             if ( $paged == 1 && ++$counter == 1) {
                // Put the code for the first post below this line. ?>
             <?php } else {
                // Put the code for the other post below this line. ?>
          <?php endif;
       endwhile; endif; ?>
       <?php posts_nav_link(' — ', __('« Newer Posts'), __('Older Posts »')); ?>
       ```
   

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘WP Query Pagination’ is closed to new replies.

## Tags

 * [pagination](https://wordpress.org/support/topic-tag/pagination/)
 * [wp_query](https://wordpress.org/support/topic-tag/wp_query/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 5 replies
 * 2 participants
 * Last reply from: [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * Last activity: [14 years ago](https://wordpress.org/support/topic/wp-query-pagination/#post-2947753)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
