Skip to content

Instantly share code, notes, and snippets.

@yokotak0527
Created February 12, 2013 02:39
Show Gist options
  • Save yokotak0527/4759796 to your computer and use it in GitHub Desktop.
Save yokotak0527/4759796 to your computer and use it in GitHub Desktop.
WordPress ページング
function paging($query_arr){
global $total_page_num;
global $paged;
$range = 5;
$half = 0;
$range_offset = 0;
$page_offset = 0;
$cnt = '<nav class="paging"><ul>';
// prev
if($paged != 1){
$cnt .= '<li class="prev"><a href="'.get_pagenum_link($paged-1).'">&#171; 前へ</a></li>';
}
// $range の調整
if($range > $total_page_num) $range = $total_page_num;
// $half 半分の位置
$half = round($range / 2);
if($paged < $half){
$page_offset = 0;
if($paged != 1) $page_offset = $paged-1;
}else{
$page_offset = $half-1;
}
if($total_page_num+$half < $range+$paged){
$range_offset = ($total_page_num+$half) - ($range+$paged);
}
for($i=$paged+$range_offset; $i<$range+$paged+$range_offset; $i++){
if($paged == ($i-$page_offset)){
$cnt .= '<li class="active"><span>'.($i-$page_offset).'</span></li>';
}else{
$cnt .= '<li class="num"><a href="'.get_pagenum_link($i-$page_offset).'">'.($i-$page_offset).'</a></li>';
}
}
// next
if($total_page_num != $paged){
$cnt .= '<li class="next"><a href="'.get_pagenum_link($paged+1).'">次へ &#187;</a></li>';
}
$cnt .= '</ul></nav>';
echo $cnt;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment