描述:
自动生成分页导航
用法:
<?php echo paginate_links( $args ); ?>
参数:
即使整个参数数组被标记为可选,如果不指定所需的参数,函数也不会输出任何内容。
base
(string) (可选) 用于引用url,该url将用于创建分页链接。“http://example.com/all_posts.php%”中的默认值“%”替换为“format”参数(请参见下文)。
默认值: '%_%'
format
(string) (可选) 用于分页结构。默认值是'?page=%35;%',如果使用漂亮的永久对齐,则为'/page/%#%',其中“%#%”替换为页码。
默认值: '?page=%#%'
total
(integer) (可选) 总页数。
默认值: 1
current
(integer) (可选) 当前页码
默认值: 0
show_all
(boolean) (可选) 如果设置为True,则它将显示所有页面,而不是当前页面附近的简短页面列表。默认情况下,“show_all”设置为false,并由“end_size”和“mid_size”参数控制。
默认值: False
end_size
(integer) (可选) 开始列表边和结束列表边上有多少个数字。
默认值: 1
mid_size
(integer) (可选) 当前页两边有多少数字,但不包括当前页。
默认值: 2
prev_next
(boolean) (可选) 是否在列表中包含上一个和下一个链接。
默认值: True
prev_text
(string) (可选) 上一页文本。仅当“prev_next”参数设置为true时才有效。
默认值: __('« Previous')
next_text
(string) (可选) 下一页的文本。仅当“prev_next”参数设置为true时才有效。
默认值: __('Next »')
type
(string) (可选) 控制返回值的格式。可能值为:
'plain' - 用换行符分隔链接的字符串。
'array' - 分页链接列表的数组,提供对显示的完全控制。
'list' - 无序的HTML列表。
默认值: 'plain'
add_args
(array) (可选) 要添加的查询参数数组。
默认值: false
add_fragment
(string) (可选) 要附加到每个链接的字符串。
默认值: None
before_page_number
(string) (可选) 出现在页码前面的字符串。
默认值: None
after_page_number
(string) (可选) 要在页码后追加的字符串。
默认值: None
示例:
<?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
) );
?>
源文件:
function paginate_links( $args = '' ) {
global $wp_query, $wp_rewrite;
// Setting up default values based on the current URL.
$pagenum_link = html_entity_decode( get_pagenum_link() );
$url_parts = explode( '?', $pagenum_link );
// Get max pages and current page out of the current query, if available.
$total = isset( $wp_query->max_num_pages ) ? $wp_query->max_num_pages : 1;
$current = get_query_var( 'paged' ) ? intval( get_query_var( 'paged' ) ) : 1;
// Append the format placeholder to the base URL.
$pagenum_link = trailingslashit( $url_parts[0] ) . '%_%';
// URL base depends on permalink settings.
$format = $wp_rewrite->using_index_permalinks() && ! strpos( $pagenum_link, 'index.php' ) ? 'index.php/' : '';
$format .= $wp_rewrite->using_permalinks() ? user_trailingslashit( $wp_rewrite->pagination_base . '/%#%', 'paged' ) : '?paged=%#%';
$defaults = array(
'base' => $pagenum_link, // http://example.com/all_posts.php%_% : %_% is replaced by format (below)
'format' => $format, // ?page=%#% : %#% is replaced by the page number
'total' => $total,
'current' => $current,
'aria_current' => 'page',
'show_all' => false,
'prev_next' => true,
'prev_text' => __( '« Previous' ),
'next_text' => __( 'Next »' ),
'end_size' => 1,
'mid_size' => 2,
'type' => 'plain',
'add_args' => array(), // array of query args to add
'add_fragment' => '',
'before_page_number' => '',
'after_page_number' => '',
);
$args = wp_parse_args( $args, $defaults );
if ( ! is_array( $args['add_args'] ) ) {
$args['add_args'] = array();
}
// Merge additional query vars found in the original URL into 'add_args' array.
if ( isset( $url_parts[1] ) ) {
// Find the format argument.
$format = explode( '?', str_replace( '%_%', $args['format'], $args['base'] ) );
$format_query = isset( $format[1] ) ? $format[1] : '';
wp_parse_str( $format_query, $format_args );
// Find the query args of the requested URL.
wp_parse_str( $url_parts[1], $url_query_args );
// Remove the format argument from the array of query arguments, to avoid overwriting custom format.
foreach ( $format_args as $format_arg => $format_arg_value ) {
unset( $url_query_args[ $format_arg ] );
}
$args['add_args'] = array_merge( $args['add_args'], urlencode_deep( $url_query_args ) );
}
// Who knows what else people pass in $args
$total = (int) $args['total'];
if ( $total < 2 ) {
return;
}
$current = (int) $args['current'];
$end_size = (int) $args['end_size']; // Out of bounds? Make it the default.
if ( $end_size < 1 ) {
$end_size = 1;
}
$mid_size = (int) $args['mid_size'];
if ( $mid_size < 0 ) {
$mid_size = 2;
}
$add_args = $args['add_args'];
$r = '';
$page_links = array();
$dots = false;
if ( $args['prev_next'] && $current && 1 < $current ) :
$link = str_replace( '%_%', 2 == $current ? '' : $args['format'], $args['base'] );
$link = str_replace( '%#%', $current - 1, $link );
if ( $add_args ) {
$link = add_query_arg( $add_args, $link );
}
$link .= $args['add_fragment'];
$page_links[] = sprintf(
'<a class="prev page-numbers" href="%s">%s</a>',
/**
* Filters the paginated links for the given archive pages.
*
* @since 3.0.0
*
* @param string $link The paginated link URL.
*/
esc_url( apply_filters( 'paginate_links', $link ) ),
$args['prev_text']
);
endif;
for ( $n = 1; $n <= $total; $n++ ) :
if ( $n == $current ) :
$page_links[] = sprintf(
'<span aria-current="%s" class="page-numbers current">%s</span>',
esc_attr( $args['aria_current'] ),
$args['before_page_number'] . number_format_i18n( $n ) . $args['after_page_number']
);
$dots = true;
else :
if ( $args['show_all'] || ( $n <= $end_size || ( $current && $n >= $current - $mid_size && $n <= $current + $mid_size ) || $n > $total - $end_size ) ) :
$link = str_replace( '%_%', 1 == $n ? '' : $args['format'], $args['base'] );
$link = str_replace( '%#%', $n, $link );
if ( $add_args ) {
$link = add_query_arg( $add_args, $link );
}
$link .= $args['add_fragment'];
$page_links[] = sprintf(
'<a class="page-numbers" href="%s">%s</a>',
/** This filter is documented in wp-includes/general-template.php */
esc_url( apply_filters( 'paginate_links', $link ) ),
$args['before_page_number'] . number_format_i18n( $n ) . $args['after_page_number']
);
$dots = true;
elseif ( $dots && ! $args['show_all'] ) :
$page_links[] = '<span class="page-numbers dots">' . __( '…' ) . '</span>';
$dots = false;
endif;
endif;
endfor;
if ( $args['prev_next'] && $current && $current < $total ) :
$link = str_replace( '%_%', $args['format'], $args['base'] );
$link = str_replace( '%#%', $current + 1, $link );
if ( $add_args ) {
$link = add_query_arg( $add_args, $link );
}
$link .= $args['add_fragment'];
$page_links[] = sprintf(
'<a class="next page-numbers" href="%s">%s</a>',
/** This filter is documented in wp-includes/general-template.php */
esc_url( apply_filters( 'paginate_links', $link ) ),
$args['next_text']
);
endif;
switch ( $args['type'] ) {
case 'array':
return $page_links;
case 'list':
$r .= "<ul class='page-numbers'>nt<li>";
$r .= join( "</li>nt<li>", $page_links );
$r .= "</li>n</ul>n";
break;
default:
$r = join( "n", $page_links );
break;
}
return $r;
}
发表评论
还没有评论,快来抢沙发吧!