概要
このWEBサイトで使用しているページネーション
ソース
<?php
// 全ページ数を取得
$max_pages = $wp_query->max_num_pages;
if($max_pages > 1):
?>
<div id="pager">
<?php
// 現在のページ番号を取得
$current_page = get_query_var( 'paged' );
$current_page = $current_page === 0 ? '1' : $current_page;
if($max_pages <= 5){
$start_page = 1;
$end_page = $max_pages;
}else{
if($current_page - 2 <= 1){
$start_page = 1;
$end_page = $start_page + 4;
}else{
$start_page = $current_page - 2;
$end_page = $start_page + 4;
}
if($current_page + 2 > $max_pages){
$end_page = $max_pages;
$start_page = $max_pages - 4;
}
}
if($current_page > 1){ ?>
<div><a href="<?php echo get_pagenum_link($current_page - 1); ?>" class="prev"></a></div>
<?php } ?>
<div class="page_num"><?php echo $current_page . '/' . $max_pages; ?></div>
<?php if($start_page - 1 >= 1){ ?>
<div><a href="<?php echo get_pagenum_link(1); ?>">1</a></div>
<div>...</div>
<?php } ?>
<?php for ($i = $start_page; $i <= $end_page; $i++){ ?>
<?php if((int)$i === (int)$current_page): ?>
<div><span><?php echo $i; ?></span></div>
<?php else: ?>
<div><a href="<?php echo get_pagenum_link($i); ?>"><?php echo $i; ?></a></div>
<?php endif; ?>
<?php } ?>
<?php if($end_page + 1 <= $max_pages){ ?>
<div>...</div>
<div><a href="<?php echo get_pagenum_link($max_pages); ?>"><?php echo $max_pages; ?></a></div>
<?php } ?>
<?php if($current_page < $max_pages){ ?>
<div><a href="<?php echo get_pagenum_link($current_page + 1); ?>" class="next"></a></div>
<?php } ?>
</div>
<?php endif; ?>