Have you tried setting the prev_text
and next_text
parameters in the array you are passing to the_posts_pagination()
and also are the number of posts available more than 16?
-
This reply was modified 4 years, 6 months ago by
Shashwat.
the_posts_pagination() uses the actual loop, and not one that you define yourself, so unless you’re setting the loop up maunally and setting it to over-write the actual WordPress loop, it won’t work.
The two options you have are…
- Set your custom loop query to over-write the existing WordPress loop
- Wrie your own pagination code bsaed on your custom loop
I’d probably lean towards the second myself, just so there’s less confusion.
The the_posts_pagination()
function uses the paginate_links()
function to display the pagination. With this function you can set the total of pages from your custom query manually.
https://developer.wordpress.org/reference/functions/paginate_links/
See this example for how to set the total for your custom query.
https://developer.wordpress.org/reference/functions/paginate_links/#comment-418
-
This reply was modified 4 years, 6 months ago by
keesiemeijer.
Thanks for the responses.
@shashwatmittal I removed the prev and next text from the array because I didn’t want those in the pagination itself. There were many times that I did leave those in the array and still didn’t work.
@catacaustic Sadly, I’m not very php savvy so writing my own pagination isn’t really an option. I depend on other examples out there that I might be able to piece together and make my own.
@keesiemeijer Thanks for the links. I did try to make those examples work but I was still getting nothing returned.
Under admin -> settings -> reading I set the number of blog posts to 2 but what confuses me is should I be letting wordpress know that the photography page (being named page-photography.php) that this is my blog posts page? I do actually have a CPT archive-blog.php already. That pagination works perfectly :/
Here’s a link to the complete file: http://www.harshclimate.com/page-photography.txt
-
This reply was modified 4 years, 6 months ago by
harshclimate. Reason: Added link for reference
@harshclimate Have you tried logging the $paged
variable, is it returning a proper value, also have you used wp_reset_postdata()
anywhere, if yes is it before or after the_posts_pagination()
?
Edit: Saw wp_reset_postdata()
in the txt file you shared, can you move it after the post pagination and try once.
-
This reply was modified 4 years, 6 months ago by
Shashwat.
I did that and it didn’t change anything :/ When you say ‘logging the $paged variable’, what do you mean exactly? Can you give me an example?
Here’s the page where the pagination isn’t showing up: http://www.harshclimate.com/photography/
If you scroll down under the 16 photographs you’ll see a faint dashed line and a thick solid line. The pagination is supposed to show up in between those lines.
You can’t use the_posts_pagination()
as it’s using the global query to paginate.
Try it with this for the pagination
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $photography->max_num_pages
) );
Can you post the full code of the page template here?
I don’t see anything wrong with that.
Try changing this
<?php
the_posts_pagination( array(
'screen_reader_text' => __( 'To view more, click the links below' ),
'mid_size' => 2,
'prev_text' => __( 'back', 'textdomain' ),
'next_text' => __( 'next', 'textdomain' ),
) );
?>
with this
<?php
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $photography->max_num_pages
) );
?>
Holy crap, that worked! Thanks a lot! So why does the other way work with a CPT and not just a normal post?
You’re welcome 🙂
I’m guessing that page uses the global query ($wp_query) with the correct page total and there is no custom query in that template. If you don’t tell the WordPress pagination functions the page total (from a custom query) it’s going to default to the page total from the global query.
You can add the other arguments as well:
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $photography->max_num_pages,
'screen_reader_text' => __( 'To view more, click the links below' ),
'mid_size' => 2,
'prev_text' => __( 'back', 'textdomain' ),
'next_text' => __( 'next', 'textdomain' ),
) );
-
This reply was modified 4 years, 6 months ago by
keesiemeijer.
-
This reply was modified 4 years, 6 months ago by
keesiemeijer.
Alright cool. Appreciate all your time, people!