【Movable Type 6】テンプレート「記事リスト > 年別」を「年度別」に変更

企業サイトなどでこっそり需要のあるのが
2017年4月~2018年3月 などの年度別のリスト化です。
年度別に活動記事をまとめる などで使用します。
テンプレート年別リストにてMTEntriesと、MTArchiveListを併用して実現します。

対応方法

MTのテンプレート用意

まずは、MTのアーカイブテンプレート「記事リスト」で「年別」を使用します。

テンプレート全文

<mt:SetVarBlock name="this_year"><mt:ArchiveDate format="%Y"></mt:SetVarBlock>
<mt:SetVarBlock name="next_year"><MTSetVar name="this_year" value="1" op="+"><mt:Var name="this_year" /></mt:SetVarBlock>
<mt:Entries lastn="999" sort_by="authored_on" sort_order="ascend">
<mt:SetVarBlock name="ent_month"><MTEntryDate format="%m"></mt:SetVarBlock>
<mt:If name="ent_month" lt="4"><mt:Else>
//今の年の「4月~12月の記事」の場合に表示する内容
</mt:If></mt:Entries>
<MTArchiveList archive_type="Individual" sort_by="authored_on" sort_order="ascend">
<mt:ArchiveDate format="%Y" setvar="year">
<mt:If name="year" eq="$next_year">
<mt:ArchiveDate format="%m" setvar="month">
<mt:If name="month" ge="1"><mt:If name="month" le="3">
//次の年の「1月~3月の記事」の場合に表示する内容
</mt:If>
</mt:If></mt:If></MTArchiveList>

テンプレート解説

まずは「今の年」と「次の年」を設定

<mt:SetVarBlock name="this_year"><mt:ArchiveDate format="%Y"></mt:SetVarBlock>

日付アーカイブで年を取得。
「this_year」に格納しておきます。

<mt:ArchiveDate format="%Y" setvar="this_year"$>

という書き方にしてしまっても良いですね。

<mt:SetVarBlock name="next_year"><MTSetVar name="this_year" value="1" op="+"><mt:Var name="this_year" /></mt:SetVarBlock>

今の年である「this_year」に「1」を足した
「次の年」を「next_year」に格納しておきます。

今の年の「4月~12月の記事」

<mt:Entries lastn="999" sort_by="authored_on" sort_order="ascend">

記事を全件読み込んで(年間記事数999件を超えないブログを想定)
sort_by=”authored_on” 記事を公開日で並び替え。
sort_order=”ascend” 昇順 (新しいものが下)

<mt:SetVarBlock name="ent_month"><MTEntryDate format="%m"></mt:SetVarBlock>

記事の公開「月」を「ent_month」に格納していきます。

<mt:If name="ent_month" lt="4">

name=”ent_month” lt=”4″ 記事の公開「月」が「4」よりも小さい場合にのみ実行なのですが

<mt:Else>

指定した条件に合致しなかったときに実行するブロックタグが来ますので
「4月~12月の記事だった場合」となっています。

ここまでが年別のリストをそのまま使用した
今の年の「4月~12月の記事」のみを読み込んでいる個所になります。

次の年の「1月~3月の記事」

<MTArchiveList archive_type="Individual" sort_by="authored_on" sort_order="ascend">

アーカイブの一覧を表示するためのブロックタグ「MTArchiveList」を使用します。
rchive_type=”Individual” アーカイブの種類「記事」
sort_by=”authored_on” 記事を公開日で並び替え。
sort_order=”ascend” 昇順 (新しいものが下)

<mt:ArchiveDate format="%Y" setvar="year">

アーカイブ「記事」の公開年を「year」に格納していきます。

<mt:If name="year" eq="$next_year">

公開年が「next_year」に設定して置いた「次の年」の数字と同じだったら表示する。

<mt:ArchiveDate format="%m" setvar="month">

アーカイブ「記事」の公開月を「month」に格納していきます。

<mt:If name="month" ge="1"><mt:If name="month" le="3">

公開月が「1」以上「3」以下の一覧が出ます。

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください