<?php
$paged = $paged ?: 1;
$view_num = 10;
$the_query = new WP_Query([
'post_type' => 'blog',
'posts_per_page' => $view_num,
'paged' => $paged,
]);
$max_count = $the_query->found_posts;
if ( $max_count > $view_num ) :
$prev_url = $paged == 2 ? home_url( 'blog' ) : home_url( 'blog/page/'.($paged - 1) );
$next_url = home_url( 'blog/page/'.($paged + 1) );
?>
<div class="c-paging">
<ul class="c-paging__list">
<li class="c-paging__item"><a href="<?php echo $prev_url ?>" class="c-paging__prev u-opacity <?php echo $paged == 1 ? '-disabled' : '' ?>" aria-label="前へ"></a></li>
<?php
$paging_max_num = ceil($max_count / $view_num);
$paging_view_num = 5;
$paging_start_num = 1;
$paging_end_num = $paging_max_num > $paging_view_num ? $paging_view_num : $paging_max_num;
$paging_end_harf_num = $paging_end_num / 2;
if (ceil($paging_end_harf_num) < $paged) {
$paging_start_num = $paged - floor($paging_end_harf_num);
if ($paging_view_num < $paging_max_num) {
$paging_end_num = $paged + floor($paging_end_harf_num);
}
}
if ($paging_view_num < $paging_max_num && $paging_end_num > $paging_max_num) {
$paging_start_num -= $paging_end_num - $paging_max_num;
$paging_end_num = $paging_max_num;
}
for ( $i = $paging_start_num;$i <= $paging_end_num;$i++ ) :
$url = $i == 1 ? home_url( 'blog' ) : home_url( 'blog/page/'.$i );
?>
<li class="c-paging__item"><a href="<?php echo $url ?>" class="c-paging__number u-opacity <?php echo $i == $paged ? '-current' : '' ?>"><?php echo $i ?></a></li>
<?php endfor ?>
<li class="c-paging__item"><a href="<?php echo $next_url ?>" class="c-paging__next u-opacity <?php echo $paged == $paging_max_num ? '-disabled' : '' ?>" aria-label="次へ"></a></li>
</ul>
</div>
<?php endif ?>