wordpress自定义查询分页函数paginate_links()

WordPress教程 3540

wordpress函数paginate_links()用于任何自定义查询结果的主循环分页,包括post列表或自定义文章类型列表,或者文章存档分页、评论分页以及自定义数据等,比如想调用指定用户的所有评论并实现分页就可以通过paginate_links()函数实现。

函数代码:

1
<?php echo paginate_links( $args ); ?>
默认参数:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?php $args = array(
	'base'               => '%_%',
	'format'             => '?paged=%#%',
	'total'              => 1,
	'current'            => 0,
	'show_all'           => false,
	'end_size'           => 1,
	'mid_size'           => 2,
	'prev_next'          => true,
	'prev_text'          => __('« Previous'),
	'next_text'          => __('Next »'),
	'type'               => 'plain',
	'add_args'           => false,
	'add_fragment'       => '',
	'before_page_number' => '',
	'after_page_number'  => ''
); ?>
参数说明:
  • base – (字符串)(可选)用于引用的URL,被用来创建分页链接, 默认值“%_%”,如“http://example.com/all_posts.php%_%”,如果设置了format参数,则无效
  • format – (字符串)(可选)用于URL的分页结构,默认值“?page=%#%”,如果使用了伪静态,则是“/page/%#%”,“%#%”表示页面,例如:/page/3
  • total – (整数)(可选)总页数,默认值为1
  • current – (整数)(可选)当前页码,默认值为0
  • show_all – (布尔)(可选)是否显示所有页面,默认值是false,如果设置为true,设置为false,则调用end_size和mid_size的设置
  • end_size – (整数)(可选)分页列表两边各显示多少个页码,默认值为1
  • mid_size – (整数)(可选)当前页面页码两边的页码数量,不包括当前页码,默认值为2
  • prev_next – (布尔)(可选)是否包含上一页和下一页的链接,默认值true
  • prev_text – (字符串)(可选)前一页的文字,“prev_next”参数设置为true时生效,默认值 __(‘« Previous’)
  • next_text – (字符串)(可选)下一页的文字,“prev_next”参数设置为true时生效,默认值__(‘Next »’)
  • type – (字符串)(可选)控制返回值的格式,plain、array 或 list,默认值plain
  • add_args – (数组)(可选)添加查询字符串参数到链接,默认值false
  • add_fragment – (字符串)(可选)添加文本追加到每个链接,默认值None
  • before_page_number – (字符串)(可选)在页码前显示的字符串,默认值None
  • after_page_number – (字符串)(可选)在页码后显示的字符串,默认值None
官方示例:
1
2
3
4
5
6
7
8
9
10
11
12
<?php
global $wp_query;
 
$big = 999999999; // need an unlikely integer
 
echo paginate_links( array(
	'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
	'format' => '?paged=%#%',
	'current' => max( 1, get_query_var('paged') ),
	'total' => $wp_query->max_num_pages
));
?>
实践示例:

调用指定用户的评论列表并分页

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?php 
	$total = $wpdb->get_var("
		SELECT COUNT(comment_ID)
		FROM $wpdb->comments
		WHERE user_id = $thisauthor->ID
		AND comment_post_id = ID
		AND comment_approved = 1
	");
	$comments_per_page = 10;
	$page = isset( $_GET['cpage'] ) ? abs( (int) $_GET['cpage'] ) : 1;
	$nowPage = isset( $_GET['cpage'] ) ? abs( (int) $_GET['cpage'] ) : 1;		
	$offset = ($nowPage-1)*$comments_per_page;
	$comments = $wpdb->get_results("select * from $wpdb->comments where comment_approved = '1' AND user_id =".$user_ID." ORDER BY comment_date_gmt DESC limit ".$offset.",".$comments_per_page);
	foreach ($comments as $comment) { 
		echo $comment->comment_content;
	} 
	echo paginate_links( array(
		'base' => add_query_arg( 'cpage', '%#%' ),
		'format' => '',
		'prev_text' => __('&laquo;'),
		'next_text' => __('&raquo;'),
		'total' => ceil($total / $comments_per_page),
		'current' => $page
	));
	//The example is from http://wordpress.stackexchange.com/questions/3318/
?>

paginate_links()函数官方文档:https://codex.wordpress.org/Function_Reference/paginate_links

精品推荐: