Clients have been asking for custom amount of posts per page in certain categories. To do this, I have to create a new category template in the theme. For the ‘testimonials’ category, I created a file in my theme directory named category-testimonials.php
. The following code is meat of this page’s content:
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; query_posts( array_merge( $wp_query->query, array( 'paged' => $paged, 'cat' => 5, 'posts_per_page' => 10 ) ) ); ?> <?if ( have_posts() ) : while ( have_posts() ) : the_post();?> <?the_content();?> <?endwhile; endif;?> <?php wp_reset_query(); ?>
The $paged
checks to see if the category has more than one page, otherwise it defaults to 1 page. From there, we build the query_posts() options array.
paged
– How many pages the results will be broken intocat
– Which category I want displayed. In my case, category of if ‘5’.posts_per_page
– How many pages I want displayed per page. Every other category is set to 5 posts per page, but I want this one to be 10.
The rest of the loop is unchanged. There you have it, a category template displaying a custom amount of posts per page with pagination.
I’d prefer to adjust the original query rather than run an additional query in the view/template file. Here’s an example:
https://gist.github.com/alexkingorg/8387783