I’m having a tough time getting this to work. It’s not the first time I run into problems with pagination, or I would say with WPs URL system.
Basically I have this URL:
http://example.com/location/dc
and loads up the taxonomy-location.php
template.
Now, I’m adding the pagination feature to the theme. So I have this URL:
http://example.com/location/dc/page/2
and it won’t load the taxonomy-location.php
template, it actually loads the 404 template.
This is what the debug bar shows:
It seems like it’s getting the right values, WP is just not loading up the correct template.
EDIT (Moved over from the comment links)
functions.php
/* LOCATION */
add_rewrite_rule( 'location/([^/]+)/page/([0-9]{1,})/?$', 'index.php?location=$matches[1]&paged=$matches[2]', 'top' );
taxonomy-location.php
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts($query_string.'&posts_per_page=2&paged='.$paged);
?>
<h2>"<?= ucwords(get_query_var('location')); ?>" Venues</h2>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<article>
<h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('venue_thumb'); ?></a>
</article>
<?php endwhile; ?>
<?php wp_pagenavi(); ?>
<?php else : ?>
<article>
<h1>No posts found</h1>
</article>
<?php endif; ?>
Try adding
'paged' => get_query_var( 'page' )
to your query.EDIT : For pagination to work properly the posts per page should be greater than ‘Blog pages show at most’ under Settings-> Reading section of the WordPress Admin. So there are two ways you can make this work out.
pre_get_posts
filter.Example of pre_get_post filter to limit the no. of posts on a location taxonomy archive page to 2 posts per page.
I was running into the same issue.
Here is what I added to my functions.php to resolve the issue.
Notice that in the rewrite you must use
page=
and notpaged=
. Not sure why you can’t use paged there.Make sure you flush your rewrite rules afterward. (Re-save Permalinks)