Replace <!-- WHAT GOES HERE?????? -->
with the pagination code below:
<div class="pagination">
<?php
echo paginate_links( array(
'base' => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ),
'total' => $query->max_num_pages,
'current' => max( 1, get_query_var( 'paged' ) ),
'format' => '?paged=%#%',
'show_all' => false,
'type' => 'plain',
'end_size' => 2,
'mid_size' => 1,
'prev_next' => true,
'prev_text' => sprintf( '<i></i> %1$s', __( 'Newer Posts', 'text-domain' ) ),
'next_text' => sprintf( '%1$s <i></i>', __( 'Older Posts', 'text-domain' ) ),
'add_args' => false,
'add_fragment' => '',
) );
?>
</div>
WordPress comes with a handy function called paginate_links()
which does the heavy lifting. In the example above, the custom WP_Query object $query
is used instead of the global $wp_query
object.
Edit:
In response to comment from @mark-in-motion, here's an example of a wrapper function for paginate_links()
that I made a while back. You can add this to your theme's functions.php
, then call echo my_pagination();
in your template to display pagination.
/**
* Numeric pagination via WP core function paginate_links().
* @link http://codex.wordpress.org/Function_Reference/paginate_links
*
* @param array $args
*
* @return string HTML for numneric pagination
*/
function my_pagination( $args = array() ) {
global $wp_query;
$output = '';
if ( $wp_query->max_num_pages <= 1 ) {
return;
}
$pagination_args = array(
'base' => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ),
'total' => $wp_query->max_num_pages,
'current' => max( 1, get_query_var( 'paged' ) ),
'format' => '?paged=%#%',
'show_all' => false,
'type' => 'plain',
'end_size' => 2,
'mid_size' => 1,
'prev_next' => true,
//'prev_text' => __( '« Prev', 'text-domain' ),
//'next_text' => __( 'Next »', 'text-domain' ),
//'prev_text' => __( '‹ Prev', 'text-domain' ),
//'next_text' => __( 'Next ›', 'text-domain' ),
'prev_text' => sprintf( '<i></i> %1$s',
apply_filters( 'my_pagination_page_numbers_previous_text',
__( 'Newer Posts', 'text-domain' ) )
),
'next_text' => sprintf( '%1$s <i></i>',
apply_filters( 'my_pagination_page_numbers_next_text',
__( 'Older Posts', 'text-domain' ) )
),
'add_args' => false,
'add_fragment' => '',
// Custom arguments not part of WP core:
'show_page_position' => false, // Optionally allows the "Page X of XX" HTML to be displayed.
);
$pagination_args = apply_filters( 'my_pagination_args', array_merge( $pagination_args, $args ), $pagination_args );
$output .= paginate_links( $pagination_args );
// Optionally, show Page X of XX.
if ( true == $pagination_args['show_page_position'] && $wp_query->max_num_pages > 0 ) {
$output .= '<span class="page-of-pages">' .
sprintf( __( 'Page %1s of %2s', 'text-domain' ), $pagination_args['current'], $wp_query->max_num_pages ) .
'</span>';
}
return $output;
}