I’m trying to display posts for categories in my category.php file with pagination, but when I click the “older posts” button, I’m getting a 404. Here’s the code I’m currently using for the query:
<?php
// Get ID of category we're currently looking at
$cat = get_cat_id( single_cat_title("",false) );
query_posts(array(
'posts_per_page'=>25,
'cat' => $cat,
'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1 )
));
if(have_posts()):
?>
The permalink structure I’m using is /%category%/%postname%/
I’ve read that there is a bug that will leave you with a 404 error if the “posts_per_page” is set to less than the default, but that doesn’t seem to be the issue. The default in my settings is 20.
Any ideas? Is this an issue with the permalink settings? Shouldn’t having /category-name/page/2 work the same way as /blog-page/page/2 works?
I also get a 404 if I try to access categories like this: /category/cat-name, or /blog-page/category/cat-name
Thanks!
This sounds very similar to what I experienced (In my child theme, custom permalink %category%%postname% resulted in a 404 when using pagination in category.php, only on page 2 though).
I found this solution worked really well: http://www.bamboosolutions.co.uk/fix-404-errors-wordpress-pagination/
To summarize the solution, in my child
functions.php
file I added this bit of code:I spent so much time reading about different causes and solutions for this error, in my case it was that WP was not requesting the correct custom category. This fix saved a lot of my time!
I had the same issue, and Lauren’s fix helped me. My problem was that with this code the current page didn’t change, it got stuck on page 1.
In functions.php i added the following code:
and in category template (category.php) i used this code:
for pagination i changed this code like this. Hope my solution helps:
I would try switching to WP_Query first, it’s less buggy with Pagination.
https://codex.wordpress.org/Function_Reference/query_posts
If that doesn’t work, try changing your permalink structure to Post ID and see if that changes it.
If neither of those work, set $cat to a category you know exists (and has 26 posts) and make sure that’s not causing the problem.
Hope this helps.