How to create a WordPress post pagination without plugin
WordPress defaults on the next page and previous page links to the blog page. and pagination allows your user to page back and forth through multiple pages of content. In this tutorial How to create a WordPress post pagination without a plugin, Here I am using WordPress built-in paginate_links function.
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$data= new WP_Query(array(
'post_type'=>'posts',
'posts_per_page' => 10,
'paged' => $paged,
));
if($data->have_posts()) :
while($data->have_posts()) : $data->the_post();
// Your post code
endwhile;
$total_pages = $data->max_num_pages;
if ($total_pages > 1){
$current_page = max(1, get_query_var('paged'));
echo paginate_links(array(
'base' => get_pagenum_link(1) . '%_%',
'format' => '/page/%#%',
'current' => $current_page,
'total' => $total_pages,
'prev_text' => __('« prev'),
'next_text' => __('next »'),
));
}
?>
<?php else :?>
<h2><?php _e('404 Error Not Found', ''); ?></h2>
<?php endif; ?>
<?php wp_reset_postdata();?>