paginate タグが便利すぎた、と書いたんですが。
今朝エラーがでてて @tinybeans がみてくれて、月別アーカイブ出してるところが問題だね、ということで。
以前書いてたこちらのがぼちぼちよろしくない、ということで。
ただ、これだとエントリが多い時がなんか処理が重そうな気がするので、それは何かしら考えないといけないんだろうな。
ということで処理を見直してみた。
コード全体としてはこんな感じ
<h2>月別アーカイブ</h2>
{% spaceless %}
{% set startEntry = craft.entries().id(4127).one() %}
{% set startEntryYear = startEntry.postDate|date('Y') %}
{% set startEntryMonth = startEntry.postDate|date('n') %}
{% set nowYear = now|date('Y') %}
{% set nowMonth = now|date('n') %}
{% for year in nowYear..startEntryYear %}
<h3>{{ year }}</h3>
<ul class="list-inline">
{% set startMonth = '1' %}
{% set endMonth = '12' %}
{% if loop.first %}
{% set endMonth = nowMonth %}
{% else %}
{% set endMonth = '12' %}
{% endif %}
{% if loop.last %}
{% set startMonth = startEntryMonth %}
{% else %}
{% set startMonth = '1' %}
{% endif %}
{% for month in startMonth..endMonth %}
{% set startDate = year ~ '-' ~ month ~ '-' ~ '01' %}
{% set endDate =( startDate | date_modify('+1 month')) | date('Y-m-d') %}
{% set count = craft.entries.section('article').postDate(['and', ">= #{startDate}", "< #{endDate}"]).count() %}
{% if count != "0"%}<li class="list-inline-item"><a href="/archive/{{ year }}/{{ month }}">{{ month }}月({{count}})</a></li>{% endif %}
{% endfor %}
</ul>
{% endfor %}
{% endspaceless %}
最初(最も古い)の記事の年月を取得して、今の年月を取得しておけば、ということでそういう感じで先にセットしておく。
あとは今年から一番最初の年までループする。
そのループのなかで1月から12月までループする。
という処理だけ。
件数を取得するところを
{% set count = craft.entries.section('article').postDate(['and', ">= #{startDate}", "< #{endDate}"]).count() %}
こんなかんじでpostDateを使ってみた。とりあえず取れてるから良さそう。
countを使うようにしてみて、エラーが消えたから幾分マシになった気はする。
動的CMSの場合はこの辺もっと気をつけないとだなぁ、、、
MTだとarchivelistタグがあるから楽ちんだけど、この辺は仕方ないなー。