LoginSignup
12
16

More than 5 years have passed since last update.

[Wordpress]特定のカテゴリーで年ごとに記事一覧を表示する方法

Last updated at Posted at 2015-09-21

前回の記事で月別もしくは年別のアーカイブ(例 2015、2014、2013...)を表示する方法を紹介していますが、今回は年+記事という形式のアーカイブを表示する方法になります。

表示イメージ

  • 2015年

    • 日付
      記事の内容
    • 日付
      記事の内容
    • 日付
      記事の内容
  • 2014年

    • 日付
      記事の内容
    • 日付
      記事の内容
    • 日付
      記事の内容
  • 2013年

    • 日付
      記事の内容
    • 日付
      記事の内容
    • 日付
      記事の内容

会社のウェブサイトであれば「沿革」、アーティストさんのウェブサイトであれば「リリース情報」などに使えるのではないでしょうか。

コード

テンプレートファイルに以下のコードを記入します。

<?php
    // 出力する内容を設定
    function archiveFunc($year){
        query_posts('category_name=[カテゴリースラッグ]&posts_per_page=-1&year='.$year.'&order=ASC');
        // category_name=history などスラッグを記入
        //posts_per_page は表示する記事数(-1は無制限)

        if(have_posts()) : 
?>
<dl>
    <dt><?php echo $year; ?> 年</dt>
<?php while(have_posts()) : the_post(); ?>
    <dd>
        <div class="date"><?php the_time('n/j'); ?></div>
        <div class="content"><?php the_content(); ?></div>
    </dd>
<?php endwhile; ?>
</dl>
<?php   endif;
        wp_reset_query();
}

    $thisyear = date('Y'); // 現在の西暦年を取得

    for ($year=$thisyear; $year >= 2000; $year--) { 
        // $year >= で指定した年から現在の年までの記事を出力(例では2000年から現在まで)

        archiveFunc($year);
    }
?>

以上です。

上記で設定した関数「archiveFunc()」の詳しい使い方はこちらで以下のページでご確認ください。
https://ja.forums.wordpress.org/topic/1564

12
16
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
12
16