Created
August 11, 2014 10:29
-
-
Save apr20/6d93679117082f2b5d82 to your computer and use it in GitHub Desktop.
WordPressのページネーション(ページャー・ページナビゲーション)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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).'">« 最初</a></li>'; | |
} | |
if($paged - $range > 0){ | |
$html .= '<li><a href="'.get_pagenum_link($paged - 1).'">‹ 前へ</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).'">› 次へ</a></li>'; | |
} | |
if($paged + $range < $pages -1 ){ | |
$html .= '<li><a href="'.get_pagenum_link($pages).'">» 最後 ('.$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