投稿タイプのスラッグが「news」の場合
<?php // 年別アーカイブリストを表示
$year=NULL; // 年の初期化
$args = array( // クエリの作成
'post_type' => 'news', // 投稿タイプの指定
'orderby' => 'date', // 日付順で表示
'posts_per_page' => -1 // すべての投稿を表示
);
$the_query = new WP_Query($args); if($the_query->have_posts()){ // 投稿があれば表示
while ($the_query->have_posts()): $the_query->the_post(); // ループの開始
if ($year != get_the_date('Y')){ // 同じ年でなければ表示
$year = get_the_date('Y'); // 年の取得
echo '<a href="'.home_url( '/', 'https' ).'news/date/'.$year.'">'.$year.'年</a>'; // 年別アーカイブリストの表示
}
endwhile; // ループの終了
wp_reset_postdata(); // クエリのリセット
}
?>
投稿タイプのスラッグが「news」の場合
<?php
// 月別アーカイブリストを表示
$post_type = 'news'; // ポストタイプを指定
$args = array(
'post_type' => $post_type,
'posts_per_page' => -1
);
$archive_query = new WP_Query( $args );
while ( $archive_query->have_posts() ) {
$archive_query->the_post();
$archive_list[ get_the_time( 'Y/n', $post->ID ) ][] = $post->post_title;
}
wp_reset_postdata();
if($archive_list):
?>
<ul>
<?php
foreach( $archive_list as $year_month => $archive ) :
$year_month_arr = explode( '/', $year_month );
?>
<li>
<a href="<?php echo home_url( '/', 'https' ).$post_type.'/date/'.$year_month; ?>">
<?php echo $year_month_arr[0].'年'.$year_month_arr[1].'月' ?> [<?php echo count( $archive ) ?>]</a>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>