Title: Loops in single.php &amp; pagination
Last modified: August 21, 2016

---

# Loops in single.php & pagination

 *  [tokyobear](https://wordpress.org/support/users/tokyobear/)
 * (@tokyobear)
 * [12 years, 8 months ago](https://wordpress.org/support/topic/loops-in-singlephp/)
 * I have a problem with pagination when using a loop in a single.php file. The 
   website has the same layout as an inbox as so I want a list of the posts displayed
   at the top with the “reading pane” displaying the single post upon click. I have
   this working without issue with the following code in my single.php…
 *     ```
       //Display emails (title only)
       <?php $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
       $args = array(
       	'posts_per_page' => 20,
       	'paged'=>$paged	);
       $loop = new WP_Query( $args );
       if ( $loop->have_posts() ) :
       /* Start the Loop */
       while ( $loop->have_posts()) : $loop->the_post();
       get_template_part( 'content', get_post_format() );
       endwhile; 
   
       //Pagination
       posts_nav_link('∞','Newer emails','Older emails');
   
       endif;
   
       //Display single post
       while ( have_posts() ) : the_post();
       get_template_part( 'content-page', get_post_format() );
       endwhile;
       ```
   
 * Now this works with the list of emails above and the single email showing below…
   but I cannot get the pagination to work for the life of me. I think this is because
   for a single post it displays the content using wp_query and this is also used
   for the pagination regardless of my loop above. And so because it’s a single 
   post wp_query sees only one element and thinks there is no need for pagination(
   I may be wrong — this is just a guess).
 * I’ve tried hacking it like so:
 * `$temp = $wp_query; $wp_query = $loop;`
 * And then at the end of the main loop (before the single post is displayed)…
 * `$loop = null; $wp_query = null; $wp_query = $temp;`
 * But this just results in a fatal error…
 * Would really appreciate some help as to how to get round this! Is there a way
   I can tell pagination to use a specific loop (i.e. not wp_query)…?
 * Thanks!

Viewing 1 replies (of 1 total)

 *  Moderator [keesiemeijer](https://wordpress.org/support/users/keesiemeijer/)
 * (@keesiemeijer)
 * [12 years, 8 months ago](https://wordpress.org/support/topic/loops-in-singlephp/#post-4195829)
 * Not sure this will work in single.php but see this example how you can use pagination
   with a custom query (not using posts_nav_link()):
    [http://codex.wordpress.org/Function_Reference/next_posts_link#Usage_when_querying_the_loop_with_WP_Query](http://codex.wordpress.org/Function_Reference/next_posts_link#Usage_when_querying_the_loop_with_WP_Query)
 *     ```
       <?php
       // usage with max_num_pages
       next_posts_link( 'Older Entries', $loop->max_num_pages );
       previous_posts_link( 'Newer Entries' );
       ?>
       ```
   
 * Also, maybe you need to use `get_query_var( 'page' )` for pagination on single.
   php.

Viewing 1 replies (of 1 total)

The topic ‘Loops in single.php & pagination’ is closed to new replies.

## Tags

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

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 1 reply
 * 2 participants
 * Last reply from: [keesiemeijer](https://wordpress.org/support/users/keesiemeijer/)
 * Last activity: [12 years, 8 months ago](https://wordpress.org/support/topic/loops-in-singlephp/#post-4195829)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
