WordPress自带一个分页函数,叫做paginate_links,使用该函数就可以做文章分页。
关于paginate_links函数
<?php echo paginate_links( $args ) ?>
<?php $args = array(
'base' => '%_%',
'format' => '?page=%#%', //是否使用pretty link format
'total' => 1, //一共分多少页
'current' => 0,
'show_all' => False, //是否显示全部页码,否则将省略一部分,类似wp_pagenavi效果
'end_size' => 1, //开始和结束处显示多少个页码
'mid_size' => 2, //当前页面两边显示多少个页码
'prev_next' => True,
'prev_text' => __('« Previous'),
'next_text' => __('Next »'),
'type' => 'plain', //页码类型可以使plain,list或者array
'add_args' => False,
'add_fragment' => ); ?>对主循环进行分页
利用paginate_links函数对主循环分页,代码如下
<?php
global $wp_query;
$total = $wp_query->max_num_pages;
// only bother with the rest if we have more than 1 page!
if ( $total > 1 ) {
// get the current page
if ( !$current_page = get_query_var('paged') )
$current_page = 1;
// structure of "format" depends on whether we're using pretty permalinks
$permalink_structure = get_option('permalink_structure');
if( substr($permalink_structure,-1) != "/" ) {
if( is_home() )
$sub_format = 'page/%#%/';
else
$sub_format = '/page/%#%/';
}else {
$sub_format = 'page/%#%/';
}
$format = empty( $permalink_structure) ? '&page=%#%' : $sub_format;
echo paginate_links(array(
'base' => get_pagenum_link(1) . '%_%',
'format' => $format,
'current' => $current_page,
'total' => $total,
'mid_size' => 4,
'type' => 'plain'
));
} ?>该代码可以实现与wp_pagenavi一样的效果。
该方法不仅适用于主循环,稍微改一下还可以用于自定义查询。例如文章《利用WP User Query+WP Pavenavi制作会员列表》中提到用wp pagevani实现WP_User_Query的分页,其实用上面的代码一样可以实现,代码如下
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$paged -= 1;
$limit = 40;
$offset = $paged * $limit;
$args = array(
'number' => $limit,
'offset' => $offset,
);
// Create the WP_User_Query object
$user_query = new WP_User_Query($args);
// Get the results
$authors = $user_query->get_results();
if($authors): ?>
</pre>
<div class="members"><?php foreach($authors as $author) : ?>
<div class="user-info">
<div class="user-gravatar48"><a href="<?php the_qa_user_url( $author->ID)?>"><?php echo get_avatar($author->ID, 48); ?></a></div>
<div class="user-details"><a href="<?php the_qa_user_url( $author->ID)?>"><?php echo $author->display_name; ?></a> <?php the_qa_user_rep_inline(); ?></div>
</div>
<?php endforeach; ?><?php else: ?>
<div class="post"><?php _e('No Members yet','thematic'); ?></div>
<?php endif; ?></div>
<?php
//start page navigation
global $user_query;
$total = ceil($user_query->total_users/$limit);
// only bother with the rest if we have more than 1 page!
if ( $total > 1 ) {
// get the current page
if ( !$current_page = get_query_var('paged') )
$current_page = 1;
// structure of "format" depends on whether we're using pretty permalinks
$permalink_structure = get_option('permalink_structure');
$format = empty( $permalink_structure) ? '&page=%#%' : '/page/%#%/';
echo paginate_links(array(
'base' => get_pagenum_link(1) . '%_%',
'format' => $format,
'current' => $current_page,
'total' => $total,
'mid_size' => 4,
'type' => 'plain'
));
} ?>
我将分页写到cateogry.php的
while ( have_posts() ) :
the_post();
循环中 不行呢?
如何从主循环中得到$wp_query?分页是要依赖这个$wp_query的啊?这个值从哪里来,难道申明为global $wp_query 就会自动在某处被赋值?期待回复!!
$wp_query在加载模板之前就已经赋值了,wordpress的工作原理是先根据url判断当前是什么类型的页面,然后到数据库请求数据并给$wp_query赋值,最后调用适当的模板显示数据。模板调用在template-loader.php里。
分页还是用WP-PageNavi插件吧,省心。
文章不错 谢谢分享了!
$format = empty( $permalink_structure) ? ‘&page=%#%’ : $sub_format;
对主体分页那段代码的第20行,在我这里要改成
?paged=%#%
才有效是怎么回事?
这法子好方便的 说~~~~~适用的版本有什么限制么?
paginate_links函数基本可以对任何查询进行分页,没有使用版本的限制。
但根据具体情况的不同,需要相应调整其参数,你的情况应该跟固定连接格式有关系,是不是用的默认链接?我还没测试默认链接下这个方式好不好用,但默认链接下首页的分页应该是?paged=%#%,用&开头肯定会不对。
paginate_links中base参数里面的%_%会被format的值代替
format参数中的%#%会被实际的页码代替,你可以根据这个调整程序。