Tags

,

Add custom pagination in wordpress

pagination

paste this function in function.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";
     }
}

After that just add this to view pagination next prev button and numbers…

<div class="customPaginav">
	<div class="prev"><?php previous_posts_link(' Previous ') ?></div>
            	<?php if (function_exists("pagination")) { pagination($custom_query->max_num_pages);} ?>
	<div class="next"><?php next_posts_link(' Next ') ?></div>
</div>

adding in custom post type or any other loop

		
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
										
$wp_query = new WP_Query();
$wp_query->query(array(
	'post_type'=>'gallery',   // Post type name here
	'paged' => $paged,   // for adding pages according to the post
	'posts_per_page' => 5   // visible post on every page
));

Some CSS styling required

.customPaginav { overflow: hidden; clear: both; margin:30px 0; }
.customPaginav a { vertical-align: top; display: inline-block; font-size: 12px; border-right: 2px solid #ffffff; padding: 4px 9px; text-decoration: none; width: auto; color: #fff; background: #555; }
.customPaginav .prev, .customPaginav .next  { float: left; }
.customPaginav .prev a, .customPaginav .next a { line-height: 17px; }
.pagination { float: left; position: relative; font-size: 11px; line-height: 14px; margin: 0; }
.pagination span { display: block; float: left; margin: 0 3px 0 0; padding: 6px 9px 5px 9px; text-decoration: none; width: auto; color: #fff; background: #555; }
.pagination a { display: block; float: left; margin: 0 1px 0 0; padding: 6px 9px 5px 9px; text-decoration: none; width: auto; color: #fff; background: #555; }
.pagination a:hover { color: #fff; background: #941e22;}
Advertisements