Friday, June 12, 2015

Wordpress- WordPress Post Pagination without plugin

Step 1. Add the following function to your functions.php file:

<?php 
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 "<div class=\"pagination\"><span>Page ".$paged." of ".$pages."</span>";
         if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<a href='".get_pagenum_link(1)."'>&laquo; First</a>";
         if($paged > 1 && $showitems < $pages) echo "<a href='".get_pagenum_link($paged - 1)."'>&lsaquo; Previous</a>";
         for ($i=1; $i <= $pages; $i++)
         {
             if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems ))
             {
                 echo ($paged == $i)? "<span class=\"current\">".$i."</span>":"<a href='".get_pagenum_link($i)."' class=\"inactive\">".$i."</a>";
             }
         }
         if ($paged < $pages && $showitems < $pages) echo "<a href=\"".get_pagenum_link($paged + 1)."\">Next &rsaquo;</a>"
         if ($paged < $pages-1 &&  $paged+$range-1 < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($pages)."'>Last &raquo;</a>";
         echo "</div>\n";
     }
}
?>
Step2. To style it, add the following to your stylesheet (typically style.css).

<style>
.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;
}

</style>

Step 3: Final step, Put in your template file:


<?php
        $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
       
        $loop = new WP_Query(array('post_type' => 'post-type',
                                    'taxonomy' => 'texonomy-slug',
                                    'term'=> 'term-slug',
                                    'paged' => $paged,
                                    'posts_per_page' => 5,
                                    'orderby' => 'date',
                                    'post_status'=>'publish'                                                                               
                                    ));
 ?>


  <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
 <!-- Show loop content... -->

  <?php endwhile; ?>


<?php if (function_exists("pagination")) {
    pagination($loop->max_num_pages);
} ?>
 

7 comments :

  1. Wow, What a code you created for pagination!
    I have gone crazy just because found so easy code.
    Thanks Aftab.

    ReplyDelete
  2. Tremendous,
    First time I have ever seen so simple code for wp pagination.
    Kiss your hands.
    Thanks

    ReplyDelete
  3. Superb.
    You saved me.
    Thanks Aftab.

    ReplyDelete
  4. Wow! Bravo! Bravo!... I loved the simplicity of code and its great you posted it here. It's fantastic approach to knowledge and save time.Thanks.

    ReplyDelete
  5. Nice post.it is really very helpful for me.
    Thanks.

    ReplyDelete