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:
/
I would need it to function like this:
/
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:
/
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?
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/post_URL/
I would need it to function like this:
http://www.example/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/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?
Share Improve this question edited Apr 17, 2014 at 14:30 MxmastaMills asked Apr 11, 2014 at 23:31 MxmastaMillsMxmastaMills 2451 gold badge5 silver badges16 bronze badges 5 |5 Answers
Reset to default 1Add <!--nextpage-->
in your post editor to split your post.
When using pretty permalinks, the paged
query var is for paginated archives, and the pretty permalink format is:
http://example/category/foo/page/2/
The query var on paginated single posts is page
, and the pretty permalink format is:
http://example/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');
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
)
);
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/[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();
<?php
$paged;
if ($paged == 0){
$paged++;
} ?>
You can add some code to set the variable as 1 if it is 0.
get_query_var( 'page' )
only applies to archive index pages. For paginated single posts, you need to referenceget_query_var( 'paged' )
. – Chip Bennett Commented Apr 14, 2014 at 11:38/page/2/
to the end of the URL but that doesn't seem to work? – MxmastaMills Commented Apr 17, 2014 at 4:05