0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【WordPress】年別の記事の出し分けとページネーション

Last updated at Posted at 2019-08-21

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}\">&lt;</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}\">&lt;</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}\">&gt;</a></p>";
            }
        }
        echo "</ul>";
        echo "<p class=\"next prne nav_list\"></p>";
        echo "</div>";  
    }
}

functions.phpでページネーションを設定

スクリーンショット 2019-08-21 14.27.16.png
投稿すると画像のようになります

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?