0

I'm looking to set up pagination on my single.php file so I can have my infinite scroll function correctly. I know that pagination can be put on a single post but I somehow need to make the single.php page be able to have an appended variable on the URL. What I mean is if I have this URL:

http://www.example.com/post_URL/

I would need it to function like this:

http://www.example.com/post_URL/page/2/

It needs to function like that because I have the infinite scroll working on the homepage/main blog page that way and it's picking up the get_query_var('page') variable which doesn't seem to exist in the single.php pages. It keeps looping the first page over and over in the infinite scroll which seems to say to me that it's just defaulting to a value of '1' over and over.

Is there a way to do this?

Thanks for your help!

EDIT

I've found that I can manually make a 'page' variable by going to the URL here:

http://www.example.com/my-article/2/

The loop at the bottom of the page treats this page as if it's page = 2 even though the same content shows. My assumptions is that the 2 on the end of the page is a get_query_var() variable but it's not necessarily treated as such during my loop/infinite scroll. So I know it exists but is not accessed by the infinite scroll. What do you think the reason is for this?

EDIT

Here is the pagination code I am using on the single post page for the query (not I am using 'paged' instead 'page' which is how it should be on a single post; I have tried 'page' as well just in case):

    $paged = get_query_var('paged');


$wp_query = new WP_Query(
    array(
        'post__not_in' => array(get_the_ID()),
        'category__not_in' => 9,
        'post_type' => 'post',
        'post_status' => 'publish',
        'posts_per_page' => 3,
        'paged' => $paged
    )
);

This code works on the homepage for a loop I have there. It does not work on the single.php page though.

EDIT

So all the help has been great but it still hasn't solved the problem completely. When I echo out the $paged variable on the main page URL I get '0'. If I do it with the url with /2/ appended, it is '2', etc. (so it works then). So it's not recognizing the main page as page '1'. This is with an infinite scroll so it needs to be able to start with 1 and I'm not sure if it's possible to have the infinite scroll work on the single posts pages like that. Any idea why the main URL doesn't have a query var page value of 1?

  • get_query_var( 'page' ) only applies to archive index pages. For paginated single posts, you need to reference get_query_var( 'paged' ). – Chip Bennett Apr 14 '14 at 11:38
  • @ChipBennett that didn't do the trick...I'm assuming because I can't change pages on the single posts? I figure if there was pagination available, I'd be able to append /page/2/ to the end of the URL but that doesn't seem to work? – MxmastaMills Apr 17 '14 at 4:05
  • @YeshanSachithaKPerera, yes I have. I have a successfully loop running on another page. I've added the query code above that I am using on the single.php and that is not working (it keeps returning a value of '1' for the variable $paged when I try to print it out). It will repeat the infinite scroll but continues to repeat page 1 posts over and over again and never increments. – MxmastaMills Apr 17 '14 at 6:17
  • @MxmastaMills K buddy, Read this and that. While I'm trying to solve this problem using codes. – yeshansachithak Apr 17 '14 at 6:30
1

Add <!--nextpage--> in your post editor to split your post.

1

When using pretty permalinks, the paged query var is for paginated archives, and the pretty permalink format is:

http://example.com/category/foo/page/2/

The query var on paginated single posts is page, and the pretty permalink format is:

http://example.com/post-name/2/

If you're trying to paginate a secondary loop using the current page number in a single post, you need to read the query var page, but set the query var paged.

EDIT - grabbing the current page number on a single post:

$page = 0 == get_query_var('page') ? 1 : get_query_var('page');
  • Thank you! When I'm at the main URL of the post, get_query_var('page') is returning '0' when I echo it out. I would assume this should be '1'? – MxmastaMills Apr 17 '14 at 14:31
  • for some reason WP sets page to 0 instead of just leaving it empty like it does with paged. see edit above. – Milo Apr 17 '14 at 15:54
1

As per Milo, you need to read the query var page, but set the query var paged. Try this,

$page = 0 == get_query_var('page') ? 1 : get_query_var('page');


$wp_query = new WP_Query(
    array(
        'post__not_in' => array(get_the_ID()),
        'category__not_in' => 9,
        'post_type' => 'post',
        'post_status' => 'publish',
        'posts_per_page' => 3,
        'paged' => $paged
    )
);
  • Thank you! That sheds some light on it but for some reason when I'm at the main URL of the post, get_query_var('page') is returning '0' when I echo it out. I would assume this should be '1'? – MxmastaMills Apr 17 '14 at 14:31
  • @MxmastaMills see edit. – Rajeev Vyas Apr 18 '14 at 5:44
  • Ok so that makes sense but the issue is that the page variables aren't incrementing when the next set of posts are loading. It's starting at '1' but it just keeps repeating the same page in the pagination. Could this be because the qet_query_var('page') is always rendering as 0 (even though we are then altering it to 1)? – MxmastaMills Apr 28 '14 at 5:00
0

I was running into this same problem. Using the query_var of "page" (instead of "paged"), I could, as MxmastaMills noted, get the content to page if I manually constructed the URL like so:

www.mydomain.com/[post-type]/[post-slug]/[page]/

However, I still needed the pagination buttons to work and next_posts_link() and previous_posts_link() don't work on a single post template. I couldn't find any solutions to this anywhere, so I created my own pagination links and they work. I'm sharing in case this helps someone else in the same boat.

// using "page" instead of "paged" here
$paged = (get_query_var('page')) ? get_query_var('page') : 1;   


// The Query
$args = array(
      'post_type' => 'my-cpt',
      'post_status' => 'publish',
      'posts_per_page'     => 10,
      'paged'=>$paged,             
      'orderby' => 'date',
      'order' => 'DESC'                        
);  

$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ):

    while ( $the_query->have_posts() ):
        $the_query->the_post(); 

        // do your display

    endwhile;   

    // used in pagination buttons   
    $total_pages = $the_query->max_num_pages;   

    ?><div class="posts-navigation clearfix">                                    
        <div class="nav-previous">
            <?php 
            $previous_page = $paged + 1;
            $previous_page .= "/";
            if($paged > 0 && $paged < $total_pages): ?>
                <a href="<?php echo esc_url($expert_url . $previous_page); ?>">Older Posts</a>
            <?php endif; ?>         
        </div>
        <div class="nav-next">
            <?php 
            $next_page = $paged - 1;
            if($next_page == 1) { 
                $next_page = ""; // if the first page, don't include the "1/" at the end of the URL
            } else { 
                $next_page .= "/"; 
            }
            if($paged > 1 && $paged <= $total_pages): ?>
                <a href="<?php echo esc_url($expert_url . $next_page); ?>">Newer Posts</a>
            <?php endif; ?>                 
           </div>
    </div><?php     

endif;  

/* Restore original Post Data */
wp_reset_postdata();

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.