The main query runs before the template is loaded, and WordPress decides what template to load based on the results of that query.
You say your default posts_per_page
is set to 200, and there are 180 posts in that category, so as far as WordPress knows, there is no page 2. Your custom query that you run in the template with a different posts per page setting is unrelated to the main query.
The solution is to make adjustments to the main query before the template is loaded, via the pre_get_posts
action. This would go in your theme’s functions.php
file:
function category_posts_per_page( $query ) {
if ( !is_admin()
&& $query->is_category()
&& $query->is_main_query() ) {
$query->set( 'posts_per_page', 5 );
}
}
add_action( 'pre_get_posts', 'category_posts_per_page' );
You can then run the normal loop in the template and pagination will work as expected. Also see is_category()
in Codex if you want this to apply to a specific single category.