Advanced custom fieldを使用
releases.php
<div class="releases_list">
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$query = new WP_Query(array(
'posts_per_page' => 4,
'post_type' => 'releases',
'orderby' => 'date',
'paged' => $paged
)); ?>
<?php $prev_year = null; ?>
<?php if ($query -> have_posts()): ?><?php while ($query -> have_posts()) : $query -> the_post(); ?>
<?php $this_year = get_the_date('Y'); ?>
<?php if ($prev_year != $this_year): ?>
<div class="releases_list_year"><p><?php echo $this_year; ?></p></div>
<?php endif; ?>
<ul class="releases_list_news">
<li class="releases_list_news_list">
<dl class="releases_list_news_dl">
<dt class="releases_list_news_ttl"><p><?php the_time('Y-n-j'); ?></p></dt>
<dd class="releases_list_news_read">
<a><?php the_field('news'); ?></a>
</dd>
</dl>
</li>
</ul>
<?php $prev_year = $this_year; ?>
<?php endwhile; ?><?php endif; ?>
</div>
<div class="pagi">
<?php custom_pagination('/releases/', $paged, $query->max_num_pages); ?>
</div>
参考サイト:
https://stackoverflow.com/questions/3397725/wordpress-how-can-i-display-post-link-group-by-year
functions.php
function custom_pagination($uri, $current, $last, $range = 5){
$showitems = ($range * 2)+1;
if(preg_match('/\\?/', $uri)){
$uri .= '&';
} else {
$uri .= '?';
}
if(1 !== (int)$last){
$prev = $current-1;
$next = $current+1;
$currentPage = false;
$beforePage = false;
$afterNum = 0;
$path = get_template_directory_uri();
echo "<div class='page_nsv'>";
// echo "<p class=\"prev prne nav_list\"><a href=\"{$uri}paged={$prev}\"><</a></p>";
echo "<ul class=\"nav_list nav_con_list dot_left dot_right\">";
for ($i=1; $i <= $last; $i++){
if($i === 1 && $current !== 1){
echo "<p class=\"prev prne nav_list\"><a href=\"{$uri}paged={$prev}\"><</a></p>";
}
if($current === $i){
echo "<li class=\"nav_con_list_li\"><a href=\"#\" onclick=\"return false;\" class=\"page-link\">{$current}</a></li>";
} else {
echo "<li class=\"nav_con_list_li\"><a href=\"{$uri}paged={$i}\" class=\"page-link\">{$i}</a></li>";
}
if($i == $last && $current != $last){
echo "<li class=\"nav_con_list_li\"><a class=\"page-link\" href=\"{$uri}paged={$next}\"></a></li><p class=\"next prne nav_list\"></p>";
echo "<p class=\"next prne nav_list\"><a href=\"{$uri}paged={$next}\">></a></p>";
}
}
echo "</ul>";
echo "<p class=\"next prne nav_list\"></p>";
echo "</div>";
}
}
functions.phpでページネーションを設定