简体   繁体   English

自定义页面模板上的Wordpress分页

[英]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. 我的问题是pagenavi列出了正确的页面数,但是单击它们不会显示较旧的帖子,它只是刷新查询返回的第一篇帖子。

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. 我已经使用http://scribu.net/wordpress/wp-pagenavi/right-way-to-use-query_posts.html帮助来完成此操作,但并没有帮助。

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. 在设置查询之前,请通过设置$paged查询变量来确定正在查看的$paged

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

That basically says get a paged variable or otherwise default to 1 . 基本上就是说获取分页变量,否则默认为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. 并且以防万一,您想对一个不会改变主循环的自定义查询进行分页时,也可以将查询直接传递给wp-pagenavi() ,它将帮助您提供分页界面。 Scribu wrote about doing that in this post. Scribu在这篇文章中写了关于这样做的信息。

Hope that helps! 希望有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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