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; ?>
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('← Older Entries') ?></div>
<div class="alignright"><? previous_posts_link('Newer Entries →') ?></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(); } ?>

6:34 pm
//
Fern
I’ve spent hours trying to figure this out following other people’s tutorials. I wish I had found yours first. After 10 minutes of cutting and pasting your code and tweaking it to match the formatting of my site, I have a page that pulls posts from a single category. Thank you!!!
7:46 pm
//
tammyhart
no problem, let me know if I can be of any further assistance!
2:45 pm
//
Nicholas Pufal
I’m not able to see the pagination. The only thing that happens, is that only the number of posts i’ve set in “&showposts=” is displayed, but no pagination (no code is being generated).
I’m using the latest version, 2.8.4, of WordPress.
This is my query:
$wp_query->query(‘category_name=noticias&showposts=2′.’&paged=’.$paged);
Also, where is $paged initiated?
Thanks.
1:14 pm
//
tammyhart
Do you have the navigation links put in after the loop as described at the end of this tutorial?
2:55 pm
//
Nicholas Pufal
Sorry tammy, my bad.
I was putting the navigation links below the , so the custom query was set to null, therefore no pagination.
Thanks a lot for this! Works like a charm now.
2:56 pm
//
Nicholas Pufal
PHP code didn’t appear, but I did reference to the last line of php code.
$wp_query = null; $wp_query = $temp
3:05 pm
//
tammyhart
ah, sorry ’bout that. glad it’s working for u!
5:05 pm
//
jtrigsby
GREAT tutorial Tammy! I’ve been trying to figure this out for a while now. Still working through the custom query stuff but, hey… one step at a time.
Now then… what I’d REALLY like to do is have a different page layout for selected pages (ie single sidebar for some, double sidebar for others). From the poking around I’ve done, it looks like that layout is in the CSS file… another cold dark cave of fear and anxiety for me!
Is this something thats in the realm of reality for a mere mortal such as I? Any pointers in the right direction?
Thanks in advance,
@jtrigsby
5:24 pm
//
tammyhart
You can follow the first steps mentioned here to create a custom page template that includes a second sidebar, or you could use conditional tags in the sidebar.php file to say like “If it’s page 6, insert the following code” and then put in the second sidebar. I would need to write more than just a comment to explain this in depth, I’ll make a note of it and try to write one up soon.
You might want to try to attend my next WordPress class. If you can’t make it down to Birmingham that weekend, you can take it online too. We’ll be covering these types of things. More info with link to register at bottom: http://wordchalk.com/
6:04 pm
//
jtrigsby
Ok, that makes sense… I’ll try that. Thanks!!
10:19 pm
//
kelly
thanks for this, the documentation is scattered at best on how to do this, I as well was able to get this working in 10 minutes or less, great!
7:57 pm
//
Matt
Tammy,
Loved the article. Thank you.
I have a question about the syntax of the query. After opening the php segment (or call?), you begin with,
$temp = $wp_query;
$wp_query= null;
Are these two statement creating a variable and setting its value to “null”? I’m not a fluent php person as you can tell.
And what follows, is this assigning a new value to this variable?
$wp_query = new WP_Query();
$wp_query->query(‘cat=3′);
Is this necessary each time you make a query?
Thanks,
Matt
8:27 pm
//
tammyhart
Matt,
To be completely honest with you, I don’t know the answers to any of your questions. I don’t write php, I just copy other’s code and make it work for what I want it to do.
This is the listed code in the codex for properly querying the posts, and that’s the best I know.
8:32 pm
//
Matt
I understand. I know just enough to confuse myself and over-complicate things. I’ll try taking a page out of your book and not try to decode it but just use it.
Thanks – great article.
Matt
8:33 pm
//
tammyhart
Thankfully, the book is checked and rechecked before it is published.
9:54 pm
//
jtrigsby
>
11:56 pm
//
tammyhart
Thanks, Thom!
8:05 am
//
Dannii
I can’t get past the first bit of making the custom template. I made one and uploaded it as exercise.php but when I edit a page/add a new page there is no drodown box in the attributes section to allow me to select my new template!
9:05 am
//
tammyhart
Did you make sure added the name just like in the example given?
>
6:18 am
//
Dannii
I added that bit just before the call for the header. I gave it the template name ‘Exercise’ and saved it as exercise.php and then uploaded it to the theme folder
8:19 am
//
tammyhart
I think I may know what the problem was. In my original code, I omitted the soace between “Template Name”. go back and make sure you have a space there. If not, that may be the problem.
12:07 pm
//
How to Add the Parent Category Class to body_class() » Tammy Hart Designs
[...] pages, but when I got to the pages that I use for custom category listgins as dicussed in “WordPress: Create a Page Template that Paginates Posts from a Certain Category“, I found out that on a single post, the body_class() function doesn’t add the [...]
9:02 am
//
Sebs
Thx alot!!!!!
Did quite some searching for the correct way to do this!
The confusion in my case was caused by the apparent different use of query_posts() put before a normal loop vs contructing a new WP_Query() object and using that in the loop itself.
WP codex certainly isn’t clear enough on this..
2:50 am
//
Shivam
Thanks for the post.. It helped
12:28 am
//
Janos Fejos
I like this tutorial but can you tell me how to add thumbnails. I’d like to know how you did your http://www.tammyhartdesigns.com/work/ page I just love it and I can’t figure out how to put those small thumbnails with excerpts( )? please post the code or loop. Thousand thanx
11:28 pm
//
Tammy Hart
Janos,
That page is using three custom fields to store image url’s associated with blog posts in the “portfolio” category. This is actually a dated method since WordPress now offers a lot better/easier ways to program this, but I haven’t been taken the time to update my methods in quite some time.
10:14 pm
//
Niko
Your’re a legend, thanks for this.
10:25 am
//
Creating a Page of Category Posts | Ronnie Sherard
[...] search or sort through the archives. Well, I found the perfect solution! I used the tips found at Tammy Hart Designs , coupled with a little bit of customization of the main loop, and found great success! You can see [...]
12:57 pm
//
L
You are brilliant. I’ve scoured the internet looking for an answer to this problem you’ve just solved for me in 20 seconds. Many thanks!!
2:20 pm
//
Tammy Hart
No problem. Glad I could help.
11:31 am
//
John J
Hey thanks very much for the tut very usefull.
It is probably really easy but how do i take the post categorized and make a side bar menu?
your help would be very much appreciated.
1:08 pm
//
Tammy Hart
I’m not sure what you mean. You want to show a list of posts in the sidebar from a category?
2:23 am
//
Chad
Just wanted to say thanks, Tammy. With this post, you’ve saved a lot of time for many novice programmers.
3:27 am
//
Erwin
As Chad said “With this post, you’ve saved a lot of time for many novice programmers”….many thanks Tammy…
10:51 am
//
Donlolo
So where exactly do i start copy and paste these codes in…
9:19 am
//
Tammy Hart
Pay special attention to that first section “Create a Page Template”.
11:32 pm
//
Unwired Tech
This will work on custom pages template? I am getting error 404 for the next pages of my custom page template…
3:46 am
//
dzul
Mine is not working. The pagination appears but when i click page 2, it still showing page 1 contents. How to fix this?
2:29 pm
//
Wyatt
Great Idea. Just one issue I’m having. The pagination is set to only show posts in the same category but on the page it shows links to other pages and not posts in the same category.
Any idea on what I can do to ensure pagination only display links to previous and next posts in the same category and not to PAGES which are not posts?
Thanks
5:30 am
//
Tim
Hi Tammy, thanks for this. I’ve put it all together and it appears to work (including Wp-PageNavi) but when I click on ‘older entries’ or a different page number I get the same list of posts. The PageNavi author has a page saying that this is a symptom of an incorrect usage of query_posts: http://scribu.net/wordpress/wp-pagenavi/right-way-to-use-query_posts.html – but you aren’t using query_posts!
Any thoughts?
6:41 am
//
Tim
Nevermind Tammy, I just used query_posts instead and pagination is working now:
11, ‘paged’ => get_query_var(‘paged’) ) );
while (have_posts()) : the_post(); ?>
Posted on
10:26 am
//
Tammy Hart
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.