category

Craft CMS で月別アーカイブへのリンクにエントリ件数を表示する

2019-04-28

月別アーカイブへのリンクを出せるようにしてみた。

2019-04-24

月ごとの記事一覧ページのこと
月ごとの記事一覧ページへのリンク
を月別アーカイブ、月別アーカイブリスト、月別アーカイブリンク、とかどう呼ぶのか?がなんかあやふやだけど。

月別アーカイブへのリンクのところに当該月のエントリが何件あるか?を出してみた。
もともとのコードがこんな感じ。

{% for year, yearlyEntries in allEntries | group("postDate | date('Y')")%}
    <h3>{{ year }}年</h3>
    <ul class="list-inline">
      {# 月ごとにグループ化したエントリをループ処理 #}
      {% for month, monthlyEntries in yearlyEntries | group("postDate | date('m')") %}
        <li class="list-inline-item"><a href="/archive/{{ year }}/{{ month }}">{{ month | replace('/^0/', '') }}月</a></li>
      {% endfor %}
    </ul>
  {% endfor %}

記事数を取得するのでグループ化したところの数を出してみる

<li class="list-inline-item"><a href="/archive/{{ year }}/{{ month }}">{{ month | replace('/^0/', '') }}月({{monthlyEntries | length}})</a></li>

length を使えばOKぽい。

エレメントクエリについて | Craft 3 ドキュメント
https://docs.craftcms.com/v3/j...

の count() を使うのかな?とか思ったりしつつ色々みてたら length でいけそうだったのでそれで。

このプラグインとかは記事数が取り出せそうなのかも?

putyourlightson/craft-entry-count: Counts and displays the number of times that an entry has been viewed in Craft CMS.
https://github.com/putyourligh...