Pagination in WordPress post without plugin has been explained in this article
Many users and specifically developers do not want to have extra additional plugins loaded up into their website for each and every function and hence take help of coding in that case.
Simple codes which can do functionality and no need of plugins in this case lead us to believe that one can easily add them and get rid of the extra plugins which cause slow down of the website since they load up in either header or footer.
Lesser plugins would mean that conflicting plugins would also result less and one would have to worry less about plugin updates in future as well.
Here are 2 methods which are simple codes given for someone to have pagination in WordPress post simply and easily.
WordPress post pagination without plugin method 1 using functions.php
if ( ! function_exists( 'post_pagination' ) ) :
function post_pagination() {
global $wp_query;
$pager = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $pager, '%#%', esc_url( get_pagenum_link( $pager ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $wp_query->max_num_pages
) );
}
endif;
One needs to use the function now wherever or in whichever template they want to display:
Number of posts can be selected from Settings>readings>Select number of posts to show. And thus the number of posts would be displayed as per your choice.
2nd method we are explaining for WordPress post pagination without plugin is for custom post types:
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts(array(
'post_type' => 'post', // You can add a custom post type if you like
'paged' => $paged,
'posts_per_page' => 10 // limit of posts
));
if ( have_posts() ) : while ( have_posts() ) : the_post();
// post content goes here...
endwhile;
post_pagination();
else :
// no posts found message goes here...
endif;
Of course one would need to still stylize them once they have been displayed.
Some other developer also has written their code along with styles hope that one works for you so we will list it as method number 3 to display WordPress post pagination without plugin:
Add the following in functions.php file:
function pagination($pages = '', $range = 4)
{
$showitems = ($range * 2)+1;
global $paged;
if(empty($paged)) $paged = 1;
if($pages == '')
{
global $wp_query;
$pages = $wp_query->max_num_pages;
if(!$pages)
{
$pages = 1;
}
}
if(1 != $pages)
{
echo "
if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "« First";
if($paged > 1 && $showitems < $pages) echo "‹ Previous";
for ($i=1; $i = $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems ))
{
echo ($paged == $i)? "".$i."":"".$i."";
}
}
if ($paged < $pages && $showitems < $pages) echo "Next ›";
if ($paged < $pages-1 && $paged+$range-1 < $pages && $showitems < $pages) echo "Last »";
echo "
\n";
}
}
And then use this code in Style.css of your theme file:
.pagination {
clear:both;
padding:20px 0;
position:relative;
font-size:11px;
line-height:13px;
}
.pagination span, .pagination a {
display:block;
float:left;
margin: 2px 2px 2px 0;
padding:6px 9px 5px 9px;
text-decoration:none;
width:auto;
color:#fff;
background: #555;
}
.pagination a:hover{
color:#fff;
background: #3279BB;
}
.pagination .current{
padding:6px 9px 5px 9px;
background: #3279BB;
color:#fff;
}
Finally calling the pagination plugin in whichever template you want to use it:
max_num_pages);
} ?>
Hope this is useful for users.
In case one wants they can use WordPress pagination plugins as well.