Top > March 20, 2008

カテゴリーアーカイブのサブカテゴリーリストに、親カテゴリーとその子カテゴリーを表示する

Movable Type のカテゴリーアーカイブに表示しているサブカテゴリーリストにおいて、自カテゴリーと、その親カテゴリーに属するカテゴリーを表示するカスタマイズです。

具体例を説明します。例えば、ブログのカテゴリーが、

 cat1
  ├ cat1-1
  ├ cat1-2
  └ cat1-3
 cat2
  ├ cat2-1
  ├ cat2-2
  └ cat2-3
 cat3
  ├ cat3-1
  ├ cat3-2
  └ cat3-3

となっていると仮定します。

cat1-1 のカテゴリーアーカイブを表示した時、サブカテゴリーリストには、

 cat1
  ├ cat1-1
  ├ cat1-2
  └ cat1-3

のみを表示します。また、cat2-2 のカテゴリーアーカイブを表示した時、サブカテゴリーリストには、

 cat2
  ├ cat2-1
  ├ cat2-2
  └ cat2-3

のみを表示します。

1.サブカテゴリーのアーカイブで親カテゴリーとそのサブカテゴリーを表示する

次のサブテンプレートを任意の位置に設定して、再構築してください。

<MTParentCategory>
<MTSubCategories include_current="1">
<MTSubCatIsFirst><ul></MTSubCatIsFirst>
<MTIfNonZero tag="MTCategoryCount">
<li><a href="<$MTCategoryArchiveLink$>" title="<$MTCategoryDescription$>"><MTCategoryLabel></a> [<$MTCategoryCount$>]
<MTElse>
<li><MTCategoryLabel>
</MTElse>
</MTIfNonZero>
<MTSubCatsRecurse>
</li>
<MTSubCatIsLast></ul></MTSubCatIsLast>
</MTSubCategories>
</MTParentCategory>

このサブテンプレートは、親カテゴリーでは何も表示されません。親カテゴリーは、一番外側の MTParentCategory タグの実行条件にマッチしない(親カテゴリーに親カテゴリーが存在しない)ためです。

2.親カテゴリーでも同じように表示する

親カテゴリーである cat1 のカテゴリーアーカイブを表示した時、サブカテゴリーリストに

 cat1
  ├ cat1-1
  ├ cat1-2
  └ cat1-3

とする場合は、先のサブテンプレートに下記の青色部分を追加します。

<MTParentCategory>
<MTSubCategories include_current="1">
<MTSubCatIsFirst><ul></MTSubCatIsFirst>
<MTIfNonZero tag="MTCategoryCount">
<li><a href="<$MTCategoryArchiveLink$>" title="<$MTCategoryDescription$>"><MTCategoryLabel></a> [<$MTCategoryCount$>]
<MTElse>
<li><MTCategoryLabel>
</MTElse>
</MTIfNonZero>
<MTSubCatsRecurse>
</li>
<MTSubCatIsLast></ul></MTSubCatIsLast>
</MTSubCategories>
</MTParentCategory>
<MTHasNoParentCategory>
<MTSubCategories include_current="1">
<MTSubCatIsFirst><ul></MTSubCatIsFirst>
<MTIfNonZero tag="MTCategoryCount">
<li><a href="<$MTCategoryArchiveLink$>" title="<$MTCategoryDescription$>"><MTCategoryLabel></a> [<$MTCategoryCount$>]
<MTElse>
<li><MTCategoryLabel>
</MTElse>
</MTIfNonZero>
<MTSubCatsRecurse>
</li>
<MTSubCatIsLast></ul></MTSubCatIsLast>
</MTSubCategories>
</MTHasNoParentCategory>

ちなみに上記のリストでは、MTSubCategories が2ヶ所出現し、その内容は全く同じです(赤色部分)。

<MTParentCategory>
<MTSubCategories include_current="1">
<MTSubCatIsFirst><ul></MTSubCatIsFirst>
<MTIfNonZero tag="MTCategoryCount">
<li><a href="<$MTCategoryArchiveLink$>" title="<$MTCategoryDescription$>"><MTCategoryLabel></a> [<$MTCategoryCount$>]
<MTElse>
<li><MTCategoryLabel>
</MTElse>
</MTIfNonZero>
<MTSubCatsRecurse>
</li>
<MTSubCatIsLast></ul></MTSubCatIsLast>
</MTSubCategories>
</MTParentCategory>
<MTHasNoParentCategory>
<MTSubCategories include_current="1">
<MTSubCatIsFirst><ul></MTSubCatIsFirst>
<MTIfNonZero tag="MTCategoryCount">
<li><a href="<$MTCategoryArchiveLink$>" title="<$MTCategoryDescription$>"><MTCategoryLabel></a> [<$MTCategoryCount$>]
<MTElse>
<li><MTCategoryLabel>
</MTElse>
</MTIfNonZero>
<MTSubCatsRecurse>
</li>
<MTSubCatIsLast></ul></MTSubCatIsLast>
</MTSubCategories>
</MTHasNoParentCategory>

次のように MTSetVarTemplate タグを使って丸めておけば、サブテンプレートの重複したマークアップを解消できます。

<mt:setvartemplate name="subcategorylist">
<MTSubCategories include_current="1">
<MTSubCatIsFirst><ul></MTSubCatIsFirst>
<MTIfNonZero tag="MTCategoryCount">
<li><a href="<$MTCategoryArchiveLink$>" title="<$MTCategoryDescription$>"><MTCategoryLabel></a> [<$MTCategoryCount$>]
<MTElse>
<li><MTCategoryLabel>
</MTElse>
</MTIfNonZero>
<MTSubCatsRecurse>
</li>
<MTSubCatIsLast></ul></MTSubCatIsLast>
</MTSubCategories>
</mt:setvartemplate>
 
<MTParentCategory>
<mt:getvar name="subcategorylist">
</MTParentCategory>
<MTHasNoParentCategory>
<mt:getvar name="subcategorylist">
</MTHasNoParentCategory>

3.カテゴリーアーカイブ以外でこのサブテンプレートを表示させない対処

いずれの方法も、リストの一番外側は、

<MTIf name="category_archive">
  :
</MTIf>

で括っておけば、他のアーカイブで表示されずに済みます。

Comments [5] | Trackbacks [0]
Now loading...
List of "Mar 20, 2008"
QRcode

現在停止中です
携帯電話からこのQRcodeを撮影することで携帯用URLを取得することができます

URI for cellular phones
ギターに入った猫
Styles
Font Size
Default
For defective color vision
Gray Scale
RGB Color
Search this site

このブログをメールで購読する by:FeedBurner

loading ...
BlogPeople
Now loading...
Syndicate this site
FeedBurner(RSS1.0/RSS2.0/Atom)
Counter
これまでのアクセス
クリエイティブ・コモンズ・ライセンス
Powered by
Movable Type 4.1
 
List Me!