Pagination in WordPress

Posted: January 31, 2015 in All Category, WordPress

 $args = array(
 'post_type' => 'wp_post',
 'paged' => $paged,
 'posts_per_page' => 2);

 $agent_selection = new WP_Query($args);

 pagination($agent_selection->max_num_pages, $range = 2); 

function pagination($pages = '', $range = 2){ 
 
 $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 '<ul class="pagination pagination_nojax">';
 echo "<li class=\"roundleft\"><a href='".get_pagenum_link($paged - 1)."'><i class=\"fa fa-angle-left\"></i></a></li>";
 
 for ($i=1; $i <= $pages; $i++)
 {
 if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems ))
 {
 if ($paged == $i){
 print '<li class="active"><a href="'.get_pagenum_link($i).'" >'.$i.'</a><li>';
 }else{
 print '<li><a href="'.get_pagenum_link($i).'" >'.$i.'</a><li>';
 }
 }
 }
 
 $prev_page= get_pagenum_link($paged + 1);
 if ( ($paged +1) > $pages){
 $prev_page= get_pagenum_link($paged );
 }else{
 $prev_page= get_pagenum_link($paged + 1);
 }
 
 
 echo "<li class=\"roundright\"><a href='".$prev_page."'><i class=\"fa fa-angle-right\"></i></a><li></ul>";
 }
}
Advertisements

Comments are closed.