Skip to content

Instantly share code, notes, and snippets.

@apr20
Created August 11, 2014 10:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save apr20/6d93679117082f2b5d82 to your computer and use it in GitHub Desktop.
Save apr20/6d93679117082f2b5d82 to your computer and use it in GitHub Desktop.
WordPressのページネーション(ページャー・ページナビゲーション)
<?php
//WordPress × Bootstrap ページネーション
//$range : 表示中のページの前後何件までリンクを表示させるか
function pagination( $range = 3 ){
//paged = 表示中のページ
global $paged;
if(!$paged){ $paged = 1;}
//pages = 総ページ数
global $wp_query;
$pages = $wp_query->max_num_pages;
if(!$pages){ $pages = 1;}
//総ページが1以上の場合のみ表示
if(1 < $pages){
$html = '<ul class="pagination">';
if($paged - $range > 1){
$html .= '<li><a href="'.get_pagenum_link(1).'">&laquo; 最初</a></li>';
}
if($paged - $range > 0){
$html .= '<li><a href="'.get_pagenum_link($paged - 1).'">&lsaquo; 前へ</a></li>';
}
for ($i=1; $i <= $pages; $i++){
if(($i <= $paged + $range) and ($i >= $paged - $range)):
if($i == $paged ){
$html .= '<li class="active"><a href="'.get_pagenum_link($i).'">'.$i.'</a></li>';
}else{
$html .= '<li><a href="'.get_pagenum_link($i).'">'.$i.'</a></li>';
}
endif;
}
if($paged + $range < $pages){
$html .= '<li><a href="'.get_pagenum_link($paged + 1).'">&rsaquo; 次へ</a></li>';
}
if($paged + $range < $pages -1 ){
$html .= '<li><a href="'.get_pagenum_link($pages).'">&raquo; 最後 ('.$pages.')</a></li>';
}
$html .= "</ul>\n";
}
echo $html;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment