How to add posts pagination without a plugin

It is quite annoying to add plugin-dependent features to your wordpress themes. So I started looking if WordPress has a built-in function for paging … and after some tests I came to the conclusion that paginate_links is the perfect (built-in function) replacement for wp-pagenavi or any other plugin with the same role.

Adding pagination with a plugin means bloating your website with unnecessary plugins, just to add something that WordPress has a built-in function for. I’ll show you how to easily add pagination using the built-in WordPress function paginate_links.

function wcr_pagination(){
    global $wp_query;

    $pagination = array(
        'base' => str_replace(999999999, '%#%', esc_url(get_pagenum_link(999999999))),
        'format' => '?page=%#%',
        'current' => max( 1, get_query_var( 'paged' ) ),
        'total' => $wp_query->max_num_pages,
        'prev_text' => '«',
        'next_text' => '»',
        'type' => 'list'
    );
	
    return paginate_links($pagination);
}

This is the output generated by the above code:

Leave a Reply

Your email address will not be published. Required fields are marked *