Problems in WordPress paginating a category template

Trying to add paging to a category template in WordPress has been a 404 error strewn pain in the neck. However, I now have the answer. Or at least: an answer

In one of my sites, I have a category that I want to present differently to the rest of the content on the site. I’ve created a category-categoryname.php template in the WordPress directory and customised it to my heart’s content. So far so good. But as soon as I add paging to query_posts, the first page displays fine, but going to /page/2 404’s.

I read through many alternate ways of editing query_posts, and each way hit the same 404 problem. Thankfully I eventually stumbled on a simple solution.

If the number of posts per page specified in the category template, is less than the post per page set in the WordPress settings (in reading), then you’ll get errors.

If posts per page in category template > posts per page in WordPress settings, then paging works.

I had my category template set to 2 posts per page, for testing purposes, whilst the wordpress setting was 10. Errors. I added a few more posts and increased the number in my category template to 11 and suddenly it all worked perfectly!

This WordPress support thread was the one that finally pointed me in the right direction.

If you want a lower number of posts in your customised category than on all other categories, you need to decrease the wordpress setting to that number and change all the other category templates to the larger value.

For the record, this is the settings I used to configure the WordPress loop for paging. It’s not the nicest way out there, but it got it working, so I’m happy.

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
	   'cat'=>155, /* use own category ID here */
	   'posts_per_page'=>11, /* use own number of posts here */

Leave a Reply

Your email address will not be published. Required fields are marked *