WordPress: Create a Page Template that Paginates Posts from a Certain Category

22Comments

September 4, 2009

attributesOne 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.

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.

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);
?>

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><?php next_posts_link('&laquo; Older Entries'); ?></div>
<div><?php previous_posts_link('Newer Entries &raquo;'); ?></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(); } ?>

22 COMMENTS

  1. 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!!!

  2. no problem, let me know if I can be of any further assistance! :)

  3. 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.

  4. Do you have the navigation links put in after the loop as described at the end of this tutorial?

  5. 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.

  6. PHP code didn’t appear, but I did reference to the last line of php code.

    $wp_query = null; $wp_query = $temp

  7. ah, sorry ’bout that. glad it’s working for u! :)

  8. 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

  9. 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/

  10. Ok, that makes sense… I’ll try that. Thanks!!

  11. 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!

  12. 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

  13. 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.

  14. 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

  15. Thankfully, the book is checked and rechecked before it is published. :)

  16. 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”?

    The first line creates a new variable called temp and sets its value equal to the value of wp_query, in other words, saving the current wp_query to the temp var

    The second line then sets wp_query to null

    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?

    The first line assigns a new instance of the WP_Query object to the var wp_query. This instantiates a new query object.

    Next line sets the query parameter of the new query object to “cat=3″. This will retrieve posts from category number three. This is the set of posts that will be available in “the loop”.

    PHP is case sensitive so you’ll need to be careful with that. It always seems to bite me in the butt!

    Hope that helps… good luck.

    @jtrigsby

  17. Thanks, Thom!

  18. 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! :(

  19. Did you make sure added the name just like in the example given?

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

  20. 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

Comments Page 1 of 212»

 

Trackbacks/Pingbacks

  1. [...] 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 [...]

Reply

Google Analytics Alternative