paginate_links()
最后更新于:2021-11-27 18:28:23
paginate_links( string|array$args=”)Retrieves paginated links for archive post pages.
参数
- $args
-
(string|array) (Optional) Array or string of arguments for generating paginated links for archives.
-
‘base’
(string) Base of the paginated url. -
‘format’
(string) Format for the pagination structure. -
‘total’
(int) The total amount of pages. Default is the value GC_Query‘smax_num_pages
or 1. -
‘current’
(int) The current page number. Default is ‘paged’ query var or 1. -
‘aria_current’
(string) The value for the aria-current attribute. Possible values are ‘page’, ‘step’, ‘location’, ‘date’, ‘time’, ‘true’, ‘false’. Default is ‘page’. -
‘show_all’
(bool) Whether to show all pages. Default false. -
‘end_size’
(int) How many numbers on either the start and the end list edges. Default 1. -
‘mid_size’
(int) How many numbers to either side of the current pages. Default 2. -
‘prev_next’
(bool) Whether to include the previous and next links in the list. Default true. -
‘prev_text’
(bool) The previous page text. Default ‘« Previous’. -
‘next_text’
(bool) The next page text. Default ‘Next »’. -
‘type’
(string) Controls format of the returned value. Possible values are ‘plain’, ‘array’ and ‘list’. Default is ‘plain’. -
‘add_args’
(array) An array of query args to add. Default false. -
‘add_fragment’
(string) A string to append to each link. -
‘before_page_number’
(string) A string to appear before the page number. -
‘after_page_number’
(string) A string to append after the page number.
Default value: ”
-
‘base’
响应
(string|array|void) String of page links or array of page links, depending on ‘type’ argument. Void if total number of pages is less than 2.
源文件
文件: gc-includes/general-template.php
function paginate_links( $args = '' ) {
global $gc_query, $gc_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( $gc_query->max_num_pages ) ? $gc_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 = $gc_rewrite->using_index_permalinks() && ! strpos( $pagenum_link, 'index.php' ) ? 'index.php/' : '';
$format .= $gc_rewrite->using_permalinks() ? user_trailingslashit( $gc_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 = gc_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] : '';
gc_parse_str( $format_query, $format_args );
// Find the query args of the requested URL.
gc_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="https://docs.gechiui.com/functions/paginate_links/%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="https://docs.gechiui.com/functions/paginate_links/%s">%s</a>',
/** This filter is documented in gc-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="https://docs.gechiui.com/functions/paginate_links/%s">%s</a>',
/** This filter is documented in gc-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;
}
<?php //Protect against arbitrary paged values $paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1; $args = array( 'posts_per_page' => 5, 'category_name' => 'gallery', 'paged' => $paged, ); $the_query = new GC_Query( $args ); ?> <!-- the loop etc.. -->
<?php global $gc_query; $big = 999999999; // need an unlikely integer $translated = __( 'Page', 'mytextdomain' ); // Supply translatable string echo paginate_links( array( 'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ), 'format' => '?paged=%#%', 'current' => max( 1, get_query_var('paged') ), 'total' => $gc_query->max_num_pages, 'before_page_number' => '<span class="screen-reader-text">'.$translated.' </span>' ) ); ?>
function gcdocs_get_paginated_links( $query ) { // When we're on page 1, 'paged' is 0, but we're counting from 1, // so we're using max() to get 1 instead of 0 $currentPage = max( 1, get_query_var( 'paged', 1 ) ); // This creates an array with all available page numbers, if there // is only *one* page, max_num_pages will return 0, so here we also // use the max() function to make sure we'll always get 1 $pages = range( 1, max( 1, $query->max_num_pages ) ); // Now, map over $pages and return the page number, the url to that // page and a boolean indicating whether that number is the current page return array_map( function( $page ) use ( $currentPage ) { return ( object ) array( "isCurrent" => $page == $currentPage, "page" => $page, "url" => get_pagenum_link( $page ) ); }, $pages ); }