First off, don’t use query_posts()
.
The current category will already be available to the category.php
template.
To modify the category query and specify the number of posts per page to display, use the pre_get_posts
hook and set the value of posts_per_page
accordingly. The code below sets posts_per_page
to 2
. Add this code to you theme’s functions.php
file:
add_action( 'pre_get_posts', 'wpse_category_posts_per_page' );
function wpse_category_posts_per_page( $query ) {
if ( is_admin() ) {
return;
}
if ( $query->is_main_query() && $query->is_category() ) {
$query->set( 'posts_per_page', 2 );
}
}
Updated category.php
template file:
<?php
/**
* The category template.
*
*/
get_header();
if ( have_posts() ) :
while ( have_posts() ) :
the_post();
the_title();
the_excerpt();
echo '<a href="' . esc_url( get_permalink() ) . '"> Read more...</a>';
endwhile;
else :
echo 'There are no posts in this category.';
endif;
?>
<div class="navigation">
<div class="alignleft"><?php previous_posts_link( '« Previous Entries' ) ?></div>
<div class="alignright"><?php next_posts_link( 'Next Entries »','' ) ?></div>
</div>
<?php
get_footer();