How to Create WordPress Pagination Without a Plugin

WordPress Pagination is a basic need for every websites using WordPress platform. There are many pagination plugin available. But, here I’m going to show you How to create/display/show WordPress Pagination without a Plugin.

 

First of all you have to define function in your functions.php file for displaying pagination in your page templates, category pages and index pages. Just put the following code in your functions.php.

[code type=”php”]
function get_pagination($range = 7){
global $paged, $wp_query;

$max_page = $wp_query->max_num_pages;

if($max_page > 1){
if(!$paged){
$paged = 1;
}

if ($paged > 1){
echo ”

  • “;
    previous_posts_link(‘ ‘);
    echo “

\n”;
}
if($max_page > $range){
if($paged < $range){
for($i = 1; $i <= ($range + 1); $i++){
echo “<li”; if($i==$paged) echo ” class=’active'”; echo “>$i”;
echo “\n”;
}
}
elseif($paged >= ($max_page – ceil(($range/2)))){
for($i = $max_page – $range; $i <= $max_page; $i++){
echo “<li”; if($i==$paged) echo ” class=’active'”; echo “>$i”;
echo “\n”;
}
}
elseif($paged >= $range && $paged < ($max_page – ceil(($range/2)))){
for($i = ($paged – ceil($range/2)); $i <= ($paged + ceil(($range/2))); $i++){
echo “<li”; if($i==$paged) echo ” class=’active'”; echo “>$i”;
echo “\n”;
}
}
}
else{
for($i = 1; $i <= $max_page; $i++){
echo “<li”; if($i==$paged) echo ” class=\”active\” “; echo “>$i”;
echo “\n”;
}
}
if ($paged != $max_page){
echo ”

  • “;
    next_posts_link(‘ ‘);
    echo “

\n”;
}

}
}
[/code]

Now question where to call the function to display pagination. Just put the following code outside the loop.

[code type=”php”]

<?php get_pagination(); ?>

[/code]

Now you can do styling in the way you want.

Duan Lingxin

Duan Lingxin

Content crafter and chief editor at Scratching Info. Also regular contributor on other major online tech platforms. Security Specialist by day and a writer by night, he does his best to instill his knowledge about tech while delivering inspiring and life changing resources through his writing,

More Posts - Website

2 thoughts on “How to Create WordPress Pagination Without a Plugin”

  1. Thanks a lot for the code. LOve to have some style, can you provide? I am not good in coding and new to wordpress as well. hope you can help. Hope to see some demo. Cheers admin!

Leave a Comment