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

---

# WP Query Pagination

 *  Resolved [OlyReeve](https://wordpress.org/support/users/olyreeve/)
 * (@olyreeve)
 * [13 years, 10 months ago](https://wordpress.org/support/topic/wp-query-pagination-1/)
 * Hi,
 * I’m trying to add pagination to a wp_query loop but I can’t get it to work, if
   anyone can help it’ll be much appreciated! Here’s my code:
 *     ```
       <?php
                   $args=array(
       			'post_type' => 'post',
       			'post_status' => 'publish',
       			'posts_per_page' => 2,
       			);
   
       			$my_query = null;
       			$my_query = new WP_Query($args);
       			if( $my_query->have_posts() ) { ?>
       			<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
       				<h3 class="category"><?php the_category(', ') ?></h3>
       				<div class="blogContainer">
       					<div class="blogpiece">
       					<div class="titleMeta"><div class="metaDeets">by <?php the_author(); ?> | <?php the_time('g:i') ?> | <?php the_time('d/m/Y') ?></div><h6><?php the_title(); ?></h6></div>
       					<div class="mainText">
       						<?php the_excerpt(); ?>
       					</div>
       					<div class="readMorebox"><a href="<?php echo get_permalink(); ?>">» Read more</a></div>
       					</div>
       				</div>
       			<?php endwhile; ?>
       									<?php posts_nav_link(' — ', __('« Newer Posts'), __('Older Posts »')); ?>
   
       			<?php }
       			wp_reset_query();
       			?>
       ```
   

Viewing 15 replies - 1 through 15 (of 17 total)

1 [2](https://wordpress.org/support/topic/wp-query-pagination-1/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/wp-query-pagination-1/page/2/?output_format=md)

 *  [martiniboy](https://wordpress.org/support/users/martiniboy/)
 * (@martiniboy)
 * [13 years, 10 months ago](https://wordpress.org/support/topic/wp-query-pagination-1/#post-3082424)
 * Try
 *     ```
       <?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1
                   $args=array(
       			'post_type' => 'post',
       			'post_status' => 'publish',
       			'posts_per_page' => 2,
                               'paged' => $paged
       			);
   
       			$my_query = null;
       			$my_query = new WP_Query($args);
       			if( $my_query->have_posts() ) { ?>
       			<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
       				<h3 class="category"><?php the_category(', ') ?></h3>
       				<div class="blogContainer">
       					<div class="blogpiece">
       					<div class="titleMeta"><div class="metaDeets">by <?php the_author(); ?> | <?php the_time('g:i') ?> | <?php the_time('d/m/Y') ?></div><h6><?php the_title(); ?></h6></div>
       					<div class="mainText">
       						<?php the_excerpt(); ?>
       					</div>
       					<div class="readMorebox"><a href="<?php echo get_permalink(); ?>">» Read more</a></div>
       					</div>
       				</div>
       			<?php endwhile; ?>
       									<?php posts_nav_link(' — ', __('« Newer Posts'), __('Older Posts »')); ?>
   
       			<?php }
       			wp_reset_query();
       			?>
       ```
   
 *  Thread Starter [OlyReeve](https://wordpress.org/support/users/olyreeve/)
 * (@olyreeve)
 * [13 years, 10 months ago](https://wordpress.org/support/topic/wp-query-pagination-1/#post-3082425)
 * Thanks Martiniboy but i got an error:
    Parse error: syntax error, unexpected 
   T_VARIABLE on line 15
 *  [martiniboy](https://wordpress.org/support/users/martiniboy/)
 * (@martiniboy)
 * [13 years, 10 months ago](https://wordpress.org/support/topic/wp-query-pagination-1/#post-3082473)
 * Whoops sorry missed ; off, that teach me using my phone to answer
    `$paged = (
   get_query_var('paged')) ? get_query_var('paged') : 1;`
 * but tested when I got back to my computer and not the solution sorry.
    I did 
   have this problem a few months back I will need to look through my code of old
   sites when I am at work tomorrow. Sorry was not any help.
 *  Thread Starter [OlyReeve](https://wordpress.org/support/users/olyreeve/)
 * (@olyreeve)
 * [13 years, 10 months ago](https://wordpress.org/support/topic/wp-query-pagination-1/#post-3082690)
 * Hey Matiniboy.
 * Thanks for trying did you have any luck in the end? I’ve tried for the life of
   me to get it working but no luck!
 * Thanks!
 *  Moderator [keesiemeijer](https://wordpress.org/support/users/keesiemeijer/)
 * (@keesiemeijer)
 * [13 years, 10 months ago](https://wordpress.org/support/topic/wp-query-pagination-1/#post-3082691)
 * On what theme template file are you trying to get pagination to work (index.php,
   category.php etc…)?
 *  Thread Starter [OlyReeve](https://wordpress.org/support/users/olyreeve/)
 * (@olyreeve)
 * [13 years, 10 months ago](https://wordpress.org/support/topic/wp-query-pagination-1/#post-3082692)
 * its a custom template file.
 *  [esmi](https://wordpress.org/support/users/esmi/)
 * (@esmi)
 * [13 years, 10 months ago](https://wordpress.org/support/topic/wp-query-pagination-1/#post-3082693)
 * Is this a secondary Loop or the only Loop?
 *  [IMICreation](https://wordpress.org/support/users/nitin_8/)
 * (@nitin_8)
 * [13 years, 10 months ago](https://wordpress.org/support/topic/wp-query-pagination-1/#post-3082694)
 * try adding this in array
    ‘paged’ => get_query_var(‘paged’)
 *  Thread Starter [OlyReeve](https://wordpress.org/support/users/olyreeve/)
 * (@olyreeve)
 * [13 years, 10 months ago](https://wordpress.org/support/topic/wp-query-pagination-1/#post-3082698)
 * It’s a single loop. This is how my code looks with your nitin_8, it still doesn’t
   work:
 *     ```
       <div class="main-content">
                           <?php
                   $args=array(
       			'post_type' => 'post',
       			'post_status' => 'publish',
       			'posts_per_page' => 10,
       			'paged' => get_query_var('paged')
       			);
   
       			$my_query = null;
       			$my_query = new WP_Query($args);
       			if( $my_query->have_posts() ) { ?>
       			<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
       				<h3 class="category"><?php the_category();?> </h3>
       				<div class="blogContainer">
       					<div class="blogpiece">
       					<div class="titleMeta"><div class="metaDeets">by <?php the_author_posts_link(); ?> | <?php the_time('g:i') ?> | <?php the_time('d/m/Y') ?></div><h6><?php the_title(); ?></h6></div>
       					<div class="mainText">
       						<?php the_excerpt(); ?>
       					</div>
       					<div class="readMorebox"><a href="<?php echo get_permalink(); ?>">&raquo; Read more</a></div>
       					</div>
       				</div>
       			<?php endwhile; ?>
       									<?php posts_nav_link(' — ', __('&laquo; Newer Posts'), __('Older Posts &raquo;')); ?>
   
       			<?php }
       			wp_reset_query();
       			?>
   
                       </div>
       ```
   
 * Thanks
 *  [esmi](https://wordpress.org/support/users/esmi/)
 * (@esmi)
 * [13 years, 10 months ago](https://wordpress.org/support/topic/wp-query-pagination-1/#post-3082700)
 * You should be using query_posts() – not WP_Query() – for the main loop/query,
 *  Thread Starter [OlyReeve](https://wordpress.org/support/users/olyreeve/)
 * (@olyreeve)
 * [13 years, 10 months ago](https://wordpress.org/support/topic/wp-query-pagination-1/#post-3082701)
 * I wish I knew how to! I’m an amateur and wouldn’t know hot to rewrite the code
   with query_posts(). Thanks for the advice though esmi.
 *  [esmi](https://wordpress.org/support/users/esmi/)
 * (@esmi)
 * [13 years, 10 months ago](https://wordpress.org/support/topic/wp-query-pagination-1/#post-3082702)
 * Replace:
 *     ```
       $my_query = null;
       $my_query = new WP_Query($args);
       ```
   
 * with `query_posts($args);` – then just use a “standard Loop”.
 *  Moderator [keesiemeijer](https://wordpress.org/support/users/keesiemeijer/)
 * (@keesiemeijer)
 * [13 years, 10 months ago](https://wordpress.org/support/topic/wp-query-pagination-1/#post-3082703)
 * Is this on a Page template, [custom page template](http://codex.wordpress.org/Pages#Creating_Your_Own_Page_Templates),
   [static front page](http://codex.wordpress.org/Creating_a_Static_Front_Page) 
   or on another theme template file?
 * Try it with this:
 *     ```
       <div class="main-content">
                           <?php
                           if ( get_query_var('paged') ) { $paged = get_query_var('paged'); }
       elseif ( get_query_var('page') ) { $paged = get_query_var('page'); }
       else { $paged = 1; }
   
                   $args=array(
       			'post_type' => 'post',
       			'post_status' => 'publish',
       			'posts_per_page' => 10,
       			'paged' => $paged
       			);
   
       			query_posts($args);
       			if( have_posts() ) : ?>
       			<?php while (have_posts()) : the_post(); ?>
       				<h3 class="category"><?php the_category();?> </h3>
       				<div class="blogContainer">
       					<div class="blogpiece">
       					<div class="titleMeta"><div class="metaDeets">by <?php the_author_posts_link(); ?> | <?php the_time('g:i') ?> | <?php the_time('d/m/Y') ?></div><h6><?php the_title(); ?></h6></div>
       					<div class="mainText">
       						<?php the_excerpt(); ?>
       					</div>
       					<div class="readMorebox"><a href="<?php echo get_permalink(); ?>">» Read more</a></div>
       					</div>
       				</div>
       			<?php endwhile; ?>
       									<?php posts_nav_link(' — ', __('« Newer Posts'), __('Older Posts »')); ?>
   
       			<?php endif; ?>
       			<?php wp_reset_query(); ?>
   
                       </div>
       ```
   
 * see: [http://codex.wordpress.org/Pagination#Troubleshooting_Broken_Pagination](http://codex.wordpress.org/Pagination#Troubleshooting_Broken_Pagination)
 *  Thread Starter [OlyReeve](https://wordpress.org/support/users/olyreeve/)
 * (@olyreeve)
 * [13 years, 10 months ago](https://wordpress.org/support/topic/wp-query-pagination-1/#post-3082704)
 * Thanks for your help people, Kessitemeijer – it didn’t work, sorry. The page 
   is a custom page template.
 * Esmi I changed what you said but it’s given me an error, have I missed something?
 *     ```
       <?php
       /*
       Template Name: Blog Template
       */
       get_header(); ?>
       <div id="main">
                 <div class="main-section">
                       <div class="headline">
                           <div class="holder">
                               <h1>Blog</h1>
                           </div>
                       </div>
                       <div class="main-content">
                           <?php
                   $args=array(
       			'post_type' => 'post',
       			'post_status' => 'publish',
       			'posts_per_page' => 10,
       			'paged' => get_query_var('paged')
       			);
   
       			query_posts($args);
       			if( $my_query->have_posts() ) { ?>
       			<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
       				<h3 class="category"><?php the_category();?> </h3>
       				<div class="blogContainer">
       					<div class="blogpiece">
       					<div class="titleMeta"><div class="metaDeets">by <?php the_author_posts_link(); ?> | <?php the_time('g:i') ?> | <?php the_time('d/m/Y') ?></div><h6><?php the_title(); ?></h6></div>
       					<div class="mainText">
       						<?php the_excerpt(); ?>
       					</div>
       					<div class="readMorebox"><a href="<?php echo get_permalink(); ?>">&raquo; Read more</a></div>
       					</div>
       				</div>
       			<?php endif; ?>
       									<?php posts_nav_link(' — ', __('&laquo; Newer Posts'), __('Older Posts &raquo;')); ?>
                       </div>
                   </div>
       </div>
       <?php get_footer();?>
       ```
   
 *  [esmi](https://wordpress.org/support/users/esmi/)
 * (@esmi)
 * [13 years, 10 months ago](https://wordpress.org/support/topic/wp-query-pagination-1/#post-3082705)
 * Try dropping keesiemeijer’s code into your custom page template.

Viewing 15 replies - 1 through 15 (of 17 total)

1 [2](https://wordpress.org/support/topic/wp-query-pagination-1/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/wp-query-pagination-1/page/2/?output_format=md)

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

## Tags

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

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 17 replies
 * 5 participants
 * Last reply from: [keesiemeijer](https://wordpress.org/support/users/keesiemeijer/)
 * Last activity: [13 years, 10 months ago](https://wordpress.org/support/topic/wp-query-pagination-1/page/2/#post-3082707)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
