
paginate_links ( $args = '' )
paginate_links: 这是一个WordPress的函数,为一个列表中的项目生成分页链接。它接受一个参数数组并返回分页链接的HTML。它可以用来给任何项目列表分页,而不仅仅是评论。
为存档的文章页面检索分页链接。
从技术上讲,该函数可用于为任何区域创建分页链接列表。base”参数用于引用网址,它将被用于创建分页链接。format”参数则用于替换页码。然而,它最有可能也是默认的,被用于存档的文章页面。
type”参数控制返回值的格式。默认的是’plain’,这只是一个字符串,其中的链接由一个换行符隔开。
字符分隔的字符串。其他可能的值是’数组’或’列表’。数组”值将返回一个分页链接列表的数组,以提供对显示的完全控制。list’值将把所有的分页链接放在一个无序的HTML列表中。
total’参数是页面的总量,是一个整数。current’参数是当前页数,也是一个整数。
一个’base’参数的例子是”http://example.com/all_posts.php%_%”,’%_%’是必须的。%_%’将被’format’参数中的内容所取代。format”参数的例子是”?page=%#%”,’%#%’也是必需的。%#%’将被替换成页码。
你可以通过设置’prev_next’参数为”true”,在列表中包含上一个和下一个链接,默认情况下是这样的。你可以设置
前一个文本,通过使用’prev_text’参数。你可以通过设置’next_text’参数来设置下一个文本。
如果’show_all’参数被设置为”true”,那么它将显示所有的页面,而不是当前页面附近的一个简短的页面列表。默认情况下,’show_all’被设置为false,由’end_size’和’mid_size’参数控制。end_size”参数是在列表的开始和结束边缘有多少个数字,默认是1。”mid_size”参数是在当前页的两侧有多少个数字,但不包括当前页。
通过使用’add_args’参数可以向链接添加查询变量,更多信息请参见add_query_arg()。
before_page_number”和”after_page_number”参数允许用户自己增加链接的内容。通常情况下,这可能是为了给编号的链接添加上下文,以便屏幕阅读器用户理解链接的内容。文本字符串被添加到页码的前后–在锚标签内。
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' ) ? (int) 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 .= implode( "</li>nt<li>", $page_links ); $r .= "</li>n</ul>n"; break; default: $r = implode( "n", $page_links ); break; } /** * Filters the HTML output of paginated links for archives. * * @since 5.7.0 * * @param string $r HTML output. * @param array $args An array of arguments. See paginate_links() * for information on accepted arguments. */ $r = apply_filters( 'paginate_links_output', $r, $args ); return $r; }