WordPress pagination on custom page template

I am trying to create a custom page template to display several posts from a category and then have pagination at the bottom that allows the viewer to go to previous posts from that category.

My code is:

Read More
$args = array ( 'category_name' => 'RAGEtothis', 'posts_per_page' => 2, 'paged' => get_query_var('page') );

query_posts( $args );

while ( have_posts() ) : the_post();
    the_content();
endwhile;

if(function_exists('wp_pagenavi')) { wp_pagenavi(); }

My problem is that the pagenavi lists the correct number of pages but clicking on them does not show the older posts, it simply refreshes the first posts that were returned by the query.

I have used the http://scribu.net/wordpress/wp-pagenavi/right-way-to-use-query_posts.html
assistance for doing this and that did not help.

What am I doing wrong here? Thank you in advance.

Related posts

Leave a Reply

1 comment

  1. Before you set up your query, figure out what page is being viewed by setting a $paged query variable.

    <?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; ?>
    

    That basically says get a paged variable or otherwise default to 1. After that you will need to pass that variable into your query to get that page of results.

    <?php
    $args = array(
      'post_type' => 'post', // this is just an example query
      'paged' => $paged
    );
    query_posts($args);
    ?>
    

    And just in case you go deeper into this topic, when you want to paginate a custom query that doesn’t alter the main loop, you can also pass your query right into wp-pagenavi() and it’ll help you provide a pagination interface for that too. Scribu wrote about doing that in this post.

    Hope that helps!