Skip to content

Instantly share code, notes, and snippets.

@takien
Last active December 21, 2017 10:53
Show Gist options
  • Save takien/7339632 to your computer and use it in GitHub Desktop.
Save takien/7339632 to your computer and use it in GitHub Desktop.
WordPress Custom Paging
<?php
$perpage = 12;
$arg = Array(
'posts_per_page'=> $perpage,
'post_type'=>'product',
'paged'=> $paged
);
$query = new WP_Query($arg);
$numrows = $query->found_posts;
if($query->have_posts())
while($query->have_posts()) : $query->the_post();
// post item here
endwhile;
//ini paging nya
custom_paging($numrows,$paged,$perpage,'paged','pagination','current',true);
///ini function custom_paging nya,
//dipisah aja, atau tempatkan di functions.php
function custom_paging($numrows,$paged,$perpage,$page_query='page',$class='paging',$currentclass='current',$echo=true,$before=''){
parse_str($_SERVER['QUERY_STRING'],$current_query);
$max_pages = ceil($numrows/$perpage);
$paged = $paged ? $paged : 1;
if($max_pages <= 1) return false;
if($echo == false) {
}
else {
echo '<ul class="'.$class.'">';
?>
<?php echo $before;?>
<li><a title="First" href="<?php echo add_query_arg($page_query,1);?>">l<</a></li>
<?php if($paged > 1) { ?>
<li><a title="Prev" href="<?php echo add_query_arg($page_query,$paged-1);?>"><</a></li>
<?php } ?>
<?php
for($i=1;$i<=$max_pages;$i++){
if(($i >= ($paged-3)) AND ($i <= ($paged+3))) { ?>
<li <?php echo ($i == $paged) ? 'class="'.$currentclass.'"': '';?>>
<a href="<?php echo add_query_arg($page_query,$i);?>"><?php echo $i;?></a></li>
<?php
}
}
?>
<?php if($paged < $max_pages) { ?>
<li><a title="Next" href="<?php echo add_query_arg($page_query,$paged+1);?>">></a></li>
<?php } ?>
<li><a title="Last" href="<?php echo add_query_arg($page_query,$max_pages);?>">>l</a></li>
<?php
echo '</ul>';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment