Languages: English • 日本語 (Add your language)
Retrieve paginated link for archive post pages. Technically, the function can be used to create paginated link list for any area ( e.g.: « Prev 1 … 3 4 5 6 7 … 9 Next » )
<?php echo paginate_links( $args ); ?>
<?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' => '' ); ?>
Even if the whole arguments array is marked optional, the function will output nothing if you do not specify the needed arguments.
To add pagination to your search results and archives, you can use the following example:
<?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 ) ); ?>
When querying a loop with new WP_Query set the 'total' parameter to the max_num_pages property of the WP_Query object.
Example of a custom query:
<?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 WP_Query( $args ); ?> <!-- the loop etc.. -->
Example of paginate_links parameters adapted to the custom query above:
<?php $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' => $the_query->max_num_pages ) ); ?>
To add context to the numbered links to ensure that screen reader users understand what the links are for:
<?php global $wp_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' => $wp_query->max_num_pages, 'before_page_number' => '<span class="screen-reader-text">'.$translated.' </span>' ) ); ?>
paginate_links() is located in wp-includes/general-template.php
.