简体   繁体   中英

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:

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

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM