Update! There is a much better method for accomplishing this. Just skim the comments on the post to see how many people have had issues with this outdated method. Learn more about creating custom category templates.

One of the most important tools WordPress provides a custom theme developer is the simple ability to create custom page templates that can be used on particular pages to get specialized results apart from the basic static page template. For instance, my homepage is just another WordPress page, but in order to get it to look differently from a regular page and the main blog page, I have created a template called “Homepage” and then in the Page attributes, I’ve selected the template for that page.

Another thing that I do a lot on custom pages is to query posts from a particular category. This especially came in handy for pages like my Portfolio. One special thing to take note of is the fact that I don’t simply list all of the entries on one page, but instead break it up into sets by paginating the results of the query. This is simple to do, and I’m going to show you how.

Create a Page Template

First off, you’ll need to start with a custom template, and the easiest way to do that is duplicate your normal page template, page.php. In this file, add the following code above the call for the header:

<?php
/*
Template Name: ANY NAME YOU WANT
*/
?>

Now you can add any customizations to it that you want and then assign that template to a page by selecting it from the “template” dropdown in the Attributes panel as is shown in the above image.

Create a Custom Loop

So now that we have our custom template, let’s put in the custom query that calls posts from the category with the ID of 3. This would typically look like this:

<?php
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query();
$wp_query->query('cat=3');
?>

<?php while ($wp_query--->have_posts()): $wp_query->the_post(); ?>

... HTML and other post stuff here...

<?php endwhile; ?>

<?php $wp_query = null; $wp_query = $temp; ?&gt

Now, we will add in a couple more variables to the query to tell it to display just 4 posts per page. Now you’re query looks like this:

<?php
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query();
$wp_query->query('cat=3&showposts=4'.'&paged='.$paged);
?>

Don’t forget Pagination

Finally, you need to include the pagination links somewhere after the <?php endwhile;? >. If you are using the default navigation, that will look like this:

<div class="navigation">
<div class="alignleft"><? next_posts_link('&larr; Older Entries') ?></div>
<div class="alignright"><? previous_posts_link('Newer Entries &rarr;') ?></div>
</div>

If you’re like me, and you prefer to use the awesome plugin by Lester Chan called WP-PageNavi, then that looks like this:

<?php if(function_exists('wp_pagenavi')) { wp_pagenavi(); } ?>