投稿日:2020/08/19
WPでカテゴリーごとの月別アーカイブを設置する方法です。
category-slug.php
menuカテゴリーの場合
★menu→希望のスラッグに変更
★cat=13→13をカテゴリーIDに変更
https://www.youlife-home.com/category/meallist/
https://sekinounyu.com/category/topics/news
※sekinounyu.comはカテゴリー絞りなし
追記コード
希望するカテゴリーの一覧ページのphpに追記します。category-slug.php
menuカテゴリーの場合
★menu→希望のスラッグに変更
★cat=13→13をカテゴリーIDに変更
<?php
$archive_list = array();
$cat_slug = 'menu';
$args = array(
'category_name' => $cat_slug,
'posts_per_page' => -1,
'order' => 'DESC',
'orderby' => 'date',
);
$archive_list = array();
$archive_query = new WP_Query( $args );
while ( $archive_query->have_posts() ) :
$archive_query->the_post();
$year = get_the_time('Y', $post->ID);
$month = get_the_time('n', $post->ID);
if(empty($archive_list[$year])):
$archive_list += array($year=>array());
endif;
if(empty($archive_list[$year][$month])):
$archive_list[$year] += array($month=>array());
endif;
$archive_list[$year][$month][] = $post->ID;
endwhile;
wp_reset_postdata();
?>
<?php
/*====================================================
月別アーカイブ
=====================================================*/
if( $archive_list ) : ?>
<section class='meallist_archive'>
<div class='container gutters qa'>
<div class='row'><div class='col span_12'>
<h2>月別アーカイブ</h2>
<?php foreach( $archive_list as $year => $archive_list_month ): ?>
<h3 class="question close"><?= $year ?>年</h3>
<ul class="cl answer">
<?php foreach( $archive_list_month as $month => $archive ) :
$month_p = sprintf('%02d', $month); ?>
<li><a href="/date/<?= "{$year}/{$month_p}" ?>/?cat=13"><?= $month; ?>月</a></li>
<?php endforeach; ?>
</ul>
<?php endforeach; ?>
</div>
</div>
</section>
<?php endif; ?>
参考
https://www.sacanova.jp/menuhttps://www.youlife-home.com/category/meallist/
https://sekinounyu.com/category/topics/news
※sekinounyu.comはカテゴリー絞りなし