Your entire template is broken because rather than modifying the main query, you threw it away and put a brand new query in:
$args = array(
'posts_per_page' => 7,
'paged' => $paged,
'cat' => $postcat // Passes the selected category to the arguments
);
$custom_query = new WP_Query( $args );
WP did a lot of work figuring out which page, how many posts, what kind of posts etc, which you have to duplicate in the new query. Again, this is a brand new query that only does what you tell it to do. It’s also very bad for performance/speed and leads to slower pages.
Instead, if we use the pre_get_posts
filter to limit the archive to 7 posts as you originally intended, all the pagination code can be removed, as can the custom query:
First, lets adjust the query to only show 7 posts in functions.php
if the main query is for a category archive:
function limit_category( $query ) {
if ( $query->is_category() && $query->is_archive() && $query->is_main_query() ) {
$query->set( 'posts_per_page', '7' );
}
}
add_action( 'pre_get_posts', 'limit_category' );
Now we can use standard pagination functions, and a normal, standard posts loop in category.php
, e.g.
if ( have_posts() ) { // if we have posts
while( have_posts() ) { // while we still have posts
the_post(); // set the current post
the_title(); // display its title
the_content(); // display its content
}
// display the pagination links
?>
<div class="nav-previous alignleft"><?php previous_posts_link( 'Older posts' ); ?> </div>
<div class="nav-next alignright"><?php next_posts_link( 'Newer posts' ); ?></div>
<?php
} else {
echo "<p>No posts found</p>";
}