アトリエロワ

サイドバーの年月アーカイブからカテゴリーを絞った記事一覧をarchive.phpで出力

functions.phpにアーカイブをカテゴリで絞り込むアクションを記載

//アーカイブをカテゴリで絞り込み
function CatArchives( $query ) {
  if ( is_admin() || ! $query->is_main_query() )
    return;
  if ( $query->is_archive() || $query->is_home() ) {
    $cat_slug = (string)filter_input(INPUT_GET, 'cat_slug'); //カテゴリスラッグを取得
    $cat_id = get_category_by_slug( $cat_slug ) -> cat_ID; //スラッグからカテゴリIDを取得
    if ( $cat_id != 0 ) { //カテゴリIDがあれば
      $query->set( 'cat', $cat_id ); //指定カテゴリで絞り込む
      $query->set( 'orderby', 'date' ); //日付降順で並び替え
    }
    return;
  }
}
add_action( 'pre_get_posts', 'CatArchives' );

上記で下記のURLができる

https://test.com/?cat_slug=news
https://test.com/2021/?cat_slug=news
https://test.com/2021/01/?cat_slug=news

サイドバーで月別および年別アーカイブ一覧+カテゴリーを出す

2023年5月
2023年4月
2023年
2022年

下記の「 ?cat_slug=news 」は絞りたいカテゴリースラッグに変更
例は スラッグ:news カテゴリーID:1

この場合、archive.phpを使う

<?php
$args = array(
    'category' => 1,       // カテゴリーIDが1の記事のみを取得
    'posts_per_page' => -1 // すべての記事を表示
);

$posts_in_category = get_posts($args);
$archive_counts_monthly = array();
$archive_counts_yearly = array();

foreach ($posts_in_category as $post) {
    setup_postdata($post);
    
    // 月別アーカイブ
    $archive_month = get_the_date('Y年n月');
    $archive_month_link = get_month_link(intval(get_the_date('Y')), intval(get_the_date('n')));
    if (!isset($archive_counts_monthly[$archive_month])) {
        $archive_counts_monthly[$archive_month] = $archive_month_link;
    }
    
    // 年別アーカイブ
    $archive_year = get_the_date('Y年');
    $archive_year_link = get_year_link(intval(get_the_date('Y')));
    if (!isset($archive_counts_yearly[$archive_year])) {
        $archive_counts_yearly[$archive_year] = $archive_year_link;
    }
}

echo '<ul>';
foreach ($archive_counts_monthly as $archive_month => $link) {
    echo '<li><a href="' . $link . '?cat_slug=news">' . $archive_month . '</a></li>';
}
echo '</ul>';

echo '<ul>';
foreach ($archive_counts_yearly as $archive_year => $link) {
    echo '<li><a href="' . $link . '?cat_slug=news">' . $archive_year . '</a></li>';
}
echo '</ul>';

wp_reset_postdata();
?>

件数もついた年月アーカイブはこちら

2023年5月(4)
2023年4月(5)
2023年(9)
2022年(15)

<?php
$cat_slug = 'news'; // カテゴリースラッグ

$args = array(
    'category' => 1,       // カテゴリーIDが1の記事のみを取得
    'posts_per_page' => -1 // すべての記事を表示
);

$posts_in_category = get_posts($args);
$archive_counts_monthly = array();
$archive_counts_yearly = array();

foreach ($posts_in_category as $post) {
    setup_postdata($post);
    
    // 月別アーカイブ
    $archive_month = get_the_date('Y年n月');
    $archive_month_link = get_month_link(intval(get_the_date('Y')), intval(get_the_date('n')));
    if (!isset($archive_counts_monthly[$archive_month])) {
        $archive_counts_monthly[$archive_month] = array(
            'link' => $archive_month_link,
            'count' => 1
        );
    } else {
        $archive_counts_monthly[$archive_month]['count']++;
    }
    
    // 年別アーカイブ
    $archive_year = get_the_date('Y年');
    $archive_year_link = get_year_link(intval(get_the_date('Y')));
    if (!isset($archive_counts_yearly[$archive_year])) {
        $archive_counts_yearly[$archive_year] = array(
            'link' => $archive_year_link,
            'count' => 1
        );
    } else {
        $archive_counts_yearly[$archive_year]['count']++;
    }
}

echo '<h2>月別アーカイブ</h2>';
echo '<ul>';
foreach ($archive_counts_monthly as $archive_month => $data) {
    echo '<li><a href="' . $data['link'] . '?cat_slug=' . $cat_slug . '">' . $archive_month . ' (' . $data['count'] . ')</a></li>';
}
echo '</ul>';

echo '<h2>年別アーカイブ</h2>';
echo '<ul>';
foreach ($archive_counts_yearly as $archive_year => $data) {
    echo '<li><a href="' . $data['link'] . '?cat_slug=' . $cat_slug . '">' . $archive_year . ' (' . $data['count'] . ')</a></li>';
}
echo '</ul>';

wp_reset_postdata();
?>

HOME > wordpress設計術 > サイドバーの年月アーカイブからカテゴリーを絞った記事一覧をarchive.phpで出力

wordpress設計術

wordpress設計術