Pagination, WP Query | Type: PHPPagination without a plugin

Truthfully, I’ve relied on WP-PageNavi for pagination for years. I recently tried Simplistic Page Navi, and I like that as well. But if you want to roll your own, this snippet will help you out.

Add this to your theme’s functions.php, then use <?php 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( ' %1$s',
            apply_filters( 'my_pagination_page_numbers_previous_text',
            __( 'Newer Posts', 'text-domain' ) )
        ),
        'next_text'    => sprintf( '%1$s ',
            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 .= '' . sprintf( __( 'Page %1s of %2s', 'text-domain' ), $pagination_args['current'], $wp_query->max_num_pages ) . '';
    }

    return $output;
}

Reference Links