MovableTypeのスニペット

グローバルモディファイアリファレンス
http://www.movabletype.jp/documentation/appendices/modifiers/

アーカイブテンプレートの名前(アーカイブテンプレートに設定される予約変数)
http://www.movabletype.jp/documentation/designer/archive-template-variable.html

アーカイブマッピングで利用するアーカイブファイル名の定義
http://www.movabletype.jp/documentation/appendices/archive-file-path-specifiers.html

href="../
href="<mt:WebsiteURL />s/
href="<mt:BlogParentWebsite><mt:WebsiteURL /></mt:BlogParentWebsite>

src="../
src="<mt:WebsiteURL />s/
src="<mt:BlogParentWebsite><mt:WebsiteURL /></mt:BlogParentWebsite>

コメント

<mt:Ignore>コメント</mt:Ignore>

サイトルート

<mt:Websites parent="1"><mt:WebsiteURL /></mt:Websites>
<mt:Blogs blog_ids="1"><mt:BlogURL /></mt:Blogs>

親サイトのURL

<mt:BlogParentWebsite><mt:WebsiteURL /></mt:BlogParentWebsite>

SP版記事のパーマリンク

<mt:BlogParentWebsite><mt:WebsiteURL /></mt:BlogParentWebsite>s<mt:BlogRelativeURL /><mt:EntryDate format="%Y/%m" />/<mt:EntryBasename separator="-" />.html
<mt:BlogParentWebsite><mt:WebsiteURL /></mt:BlogParentWebsite>s<mt:BlogRelativeURL /><mt:EntryDate format="%Y/%m" />/<mt:EntryBasename separator="-" />.php

MB版記事のパーマリンク

<mt:BlogParentWebsite><mt:WebsiteURL /></mt:BlogParentWebsite>m<mt:BlogRelativeURL /><mt:EntryDate format="%Y/%m" />/<mt:EntryBasename separator="-" />.html
<mt:BlogParentWebsite><mt:WebsiteURL /></mt:BlogParentWebsite>m<mt:BlogRelativeURL /><mt:EntryDate format="%Y/%m" />/<mt:EntryBasename separator="-" />.php

SP版記事のパーマリンク(/s/の位置変更版)

<mt:BlogURL>s/<mt:EntryDate format="%Y" />/<mt:EntryDate format="%m" />/<mt:EntryBasename separator="-" />.html

MB版記事のパーマリンク(/m/の位置変更版)

<mt:BlogURL>m/<mt:EntryDate format="%Y" />/<mt:EntryDate format="%m" />/<mt:EntryBasename separator="-" />.html

記事一覧

<mt:Entries>

記事一覧(内包するカテゴリを含む)

<mt:EntriesWithSubCategories>

or

<mt:Entries include_subcategories="1">

カテゴリ一覧を表示

<mt:TopLevelCategories>

選択された記事の属するカテゴリ名を取得

<mt:EntryCategories><mt:CategoryBasename /></mt:EntryCategories>
<mt:EntryCategories><mt:CategoryLabel /></mt:EntryCategories>

選択された記事の属するカテゴリを取得(プライマリカテゴリのみ)

<mt:EntryCategories type="primary"><mt:CategoryBasename /></mt:EntryCategories>
<mt:EntryCategories type="primary"><mt:CategoryLabel /></mt:EntryCategories>

or

<mt:EntryPrimaryCategory><mt:CategoryBasename /></mt:EntryPrimaryCategory>
<mt:EntryPrimaryCategory><mt:CategoryLabel /></mt:EntryPrimaryCategory>

記事のカテゴリをカテゴリ管理画面で並べた順で出力する

<mt:SetVarBlock name="entry_categories">,<mt:EntryCategories glue=","><mt:CategoryLabel /></mt:EntryCategories>,</mt:SetVarBlock>
<mt:SubCategories top="1">
  <mt:SetVarBlock name="category_label">,<mt:CategoryLabel />,</mt:SetVarBlock>
  <mt:If name="entry_categories" like="$category_label">
    <p><mt:CategoryBasename /></p>
  </mt:If>
</mt:SubCategories>

現在のカテゴリの最上位カテゴリ

<mt:TopLevelParent></mt:TopLevelParent>

最上位のカテゴリから現在のカテゴリまでを繰り返して表示する / カテゴリを辿る(パンくずリスト等)

<mt:ParentCategories><li><a href="<mt:CategoryArchiveLink />"><mt:CategoryLabel /></a></li></mt:ParentCategories>
<mt:ParentFolders><li><a href="<mt:FolderPath />"><mt:FolderLabel /></a></li></mt:ParentFolders>

※モディファイア「exclude_current="1"」を設定することで現在のカテゴリを除きます

前後のカテゴリを表示

<mt:CategoryPrevious><mt:CategoryLabel /></MTCategoryPrevious>
<mt:CategoryNext><mt:CategoryLabel /></MTCategoryNext>

カテゴリフィルタリング(正規表現を使用した)

<mt:If tag="CategoryLabel" like="^(aaaa|bbbb|cccc)$">
</mt:If>

カテゴリアーカイブでブログ内の記事一覧を取得(別カテゴリの記事)

<mt:Entries category="NOT not_existing_category_label">
</mt:Entries>

カテゴリアーカイブで特定カテゴリを除外して記事一覧を取得

<mt:SetVarBlock name="category_filter"><mt:CategoryLabel /> AND NOT 【除外したいカテゴリ】</mt:SetVarBlock>
<mt:Entries categories="$category_filter">
</mt:Entries>

事前に変数としてフィルタリング文字列を作っておくのがポイント。

カテゴリーとサブカテゴリーを入れ子に表示させる (mt:SubCatsRecurse)

<mt:SetVar name="depth" value="1" />
<mt:TopLevelCategories>
<mt:SubCatIsFirst><ul class="list_categories list_categories_<mt:Var name="depth" />"></mt:SubCatIsFirst>
<li><a href="<mt:CategoryArchiveLink />"><mt:CategoryLabel /></a>
<mt:SetVar name="depth" op="++" />
<mt:SubCatsRecurse />
<mt:SetVar name="depth" op="--" />
</li>
<mt:SubCatIsLast></ul></mt:SubCatIsLast>
</mt:TopLevelCategories>

カテゴリーとサブカテゴリーを入れ子に表示させる (mt:SubCatsRecurse) (特定カテゴリを除外版)

<mt:SetVar name="depth" value="1" />
<mt:TopLevelCategories>
<mt:Unless tag="CategoryLabel" eq="除外したいカテゴリ名">
<mt:SubCatIsFirst><ul class="list_categories list_categories_<mt:Var name="depth" />"></mt:SubCatIsFirst>
<li><a href="<mt:CategoryArchiveLink />"><mt:CategoryLabel /></a>
<mt:SetVar name="depth" op="++" />
<mt:SubCatsRecurse />
<mt:SetVar name="depth" op="--" />
</li>
<mt:SubCatIsLast></ul></mt:SubCatIsLast>
<mt:Else>
</ul>
</mt:Unless>
</mt:TopLevelCategories>

カテゴリーとサブカテゴリーを入れ子に表示させる (mt:SubCatsRecurse) (記事一覧つき)

<mt:SetVar name="depth" value="1" />
<mt:TopLevelCategories>
<mt:SubCatIsFirst><ul class="list_categories list_categories_<mt:Var name="depth" />"></mt:SubCatIsFirst>
<li class="item_category  item_category_<mt:Var name="depth" />"><a href="<mt:CategoryArchiveLink />"><mt:CategoryLabel /></a>
<mt:Entries lastn="0">
<mt:EntriesHeader><ul class="list_entries"></mt:EntriesHeader>
<li class="item_entry"><a href="<mt:EntryPermalink escape="html" />">■<mt:EntryTitle escape="html" /></a></li>
<mt:EntriesFooter></ul></mt:EntriesFooter>
</mt:Entries>
<mt:SetVar name="depth" op="++" />
<mt:SubCatsRecurse />
<mt:SetVar name="depth" op="--" />
</li>
<mt:SubCatIsLast></ul></mt:SubCatIsLast>
</mt:TopLevelCategories>

<!-- /スタイリング用CSS 削除する -->
<style>
    .list_categories { margin: 0 0 0 20px; }
    .list_categories li { margin: 0 0 0 20px; }
</style>

カテゴリ一覧(カテゴリアーカイブ)へのリンク

<mt:CategoryArchiveLink />

選択された記事の属するフォルダを取得

<mt:FolderBasename />
<mt:FolderLabel />

選択された記事の属する最上位フォルダを取得

<mt:TopLevelFolder><mt:FolderBasename /></mt:TopLevelFolder>
<mt:TopLevelFolder><mt:FolderLabel /></mt:TopLevelFolder>

ブログのURL(/blogurl/)

<mt:BlogRelativeURL />

ブログのURL(前後のスラッシュ抜き)(blogurl)

<mt:BlogRelativeURL regex_replace="/^/(.*)/$/","\1" />

<mt:BlogBasename /> そんなタグはないけどそんな風に使えます。

ブログ名の出力

<mt:BlogName />

ハイフン/アンダーバー選択のモディファイア

separator="-"

ブログ一覧を表示

<mt:Blogs>

モディファイア

blog_ids=""

フォルダ一覧を表示

<mt:TopLevelFolders>

記事一覧を表示

<mt:Entries>

ページ一覧を表示

<mt:Pages>

改行をそのまま出力

nl2br="xhtml"

記事の概要を表示(改行あり)

<mt:EntryExcerpt convert_breaks="1" />

記事の概要を表示(改行なし)

<mt:EntryExcerpt convert_breaks="0" />

記事の概要を表示(文字数指定 - デフォルトは40)

<mt:EntryExcerpt words="40" />

記事の概要を表示(これを推奨!)

スペースなし

<mt:EntryBody remove_html="1" strip_linefeeds="1" strip="" replace="&nbsp;","" trim_to="25+..." />

スペースあり

<mt:EntryBody remove_html="1" strip_linefeeds="1" replace="&nbsp;","" trim_to="25+..." />

インデックステンプレートのURLを取得する

<mt:IndexList>
<mt:If tag="IndexName" eq="(インデックステンプレート名)"><mt:IndexLink /></mt:If>
</mt:IndexList>

メインインデックステンプレートのURLを取得する

<mt:Link template="main_index" />

エントリーの最初の画像を呼び出す

<mt:SetVarBlock name="img"><mt:EntryAssets type="image">photo</mt:EntryAssets></mt:SetVarBlock>
<mt:If name="img" like="photo">
  <mt:EntryAssets type="image" lastn="1"><img src="<mt:AssetThumbnailURL width="100" />" alt="<mt:EntryTitle escape="html" />" /></mt:EntryAssets>
<mt:Else>
  <img src="http://dummyimage.com/100x100/ccc/fff&text=Dummy" alt="<mt:EntryTitle escape="html" />" />
</mt:If>

↓変数格納バージョン

<mt:SetVarBlock name="img"><mt:EntryAssets type="image">photo</mt:EntryAssets></mt:SetVarBlock>
<mt:If name="img" like="photo">
  <mt:SetVarBlock name="image_url"><mt:EntryAssets type="image" lastn="1"><mt:AssetThumbnailURL width="100" /></mt:EntryAssets></mt:SetVarBlock>
<mt:Else>
  <mt:SetVarBlock name="image_url">http://dummyimage.com/100x100/ccc/fff&text=Dummy</mt:SetVarBlock>
</mt:If>

カスタムフィールドに登録した画像を呼び出す

<mt:If tag="[カスタムフィールドの名前]">
  <mt:[カスタムフィールドの名前]Asset><img src="<mt:AssetThumbnailURL width="100" />" alt="<mt:EntryTitle escape="html" />" /></mt:[カスタムフィールドの名前]Asset>
<mt:Else>
  <img src="http://dummyimage.com/100x100/cccccc/ffffff&text=<mt:EntryTitle escape="url" />" alt="<mt:EntryTitle escape="html" />" />
</mt:If>

↓変数格納バージョン

<mt:If tag="[カスタムフィールドの名前]">
  <mt:SetVarBlock name="image_url"><mt:[カスタムフィールドの名前]Asset><mt:AssetThumbnailURL width="100" /></mt:[カスタムフィールドの名前]Asset></mt:SetVarBlock>
<mt:Else>
  <mt:SetVarBlock name="image_url">http://dummyimage.com/100x100/ccc/fff&text=Dummy</mt:SetVarBlock>
</mt:If>

一定数値よりも小さければそのままの画像を、大きいければ縮小した画像を出力する

<mt:SetVarBlock name="imageWidth"><mt:AssetProperty property="image_width" /></mt:SetVarBlock>
<img src="<mt:If name="imageWidth" lt="435"><mt:AssetURL /><mt:Else><mt:AssetThumbnailURL width="435" /></mt:If>" />

エントリーの日付

http://www.movabletype.jp/documentation/appendices/date-formats.html

yyyy/0m/0d

<mt:EntryDate format="%Y/%m/%d" />

yyyy年m月d日

<mt:EntryDate format="%Y年%b月%e日" strip="" />

文字列をエンコードする(グローバルモディファイア)

encode_url="1"
encode_html="1"
encode_js="1"
encode_xml="1"

文字列をエスケープする(グローバルモディファイア)

escape="html"

親のテンプレートを読み込む

<mt:Include module="テンプレート" parent="1">

"0"を敷き詰める

http://www.movabletype.jp/documentation/appendices/modifiers/zero_pad.html

zero_pad="n"

メニューなどでカテゴリを判別する

まずループ外で、そのテンプレートの属するカテゴリIDを取得

<mt:If name="category_archive"><mt:CategoryID setvar="catid" />
<mt:ElseIf name="entry_archive"><mt:EntryCategories><mt:CategoryID setvar="catid" /></mt:EntryCategories>
</mt:If>

ループ内で判別させる

<mt:If tag="CategoryID" eq="$catid"> なにかする </mt:If>

前後の記事リンクから特定のカテゴリを除外(SetVarTemplateを使用した再帰処理)

<mt:Ignore>次の記事テンプレート(再帰処理)</mt:Ignore>
<mt:SetVarTemplate name="next">
<mt:EntryNext>
<mt:EntryCategories setvar="ncats">/<mt:CategoryLabel /></mt:EntryCategories>
<mt:If name="ncats" like="/[ここに除外したいカテゴリラベル]">
<mt:Var name="next" />
<mt:Else>
<a href="<mt:EntryPermalink />"><mt:EntryTitle /></a>
</mt:If>
</mt:EntryNext>
</mt:SetVarTemplate>

<mt:Ignore>前の記事テンプレート(再帰処理)</mt:Ignore>
<mt:SetVarTemplate name="prev">
<mt:EntryPrevious>
<mt:EntryCategories setvar="pcats">/<mt:CategoryLabel /></mt:EntryCategories>
<mt:If name="pcats" like="/[ここに除外したいカテゴリラベル]">
<mt:Var name="prev" />
<mt:Else>
<a href="<mt:EntryPermalink />"><mt:EntryTitle /></a>
</mt:If>
</mt:EntryPrevious>
</mt:SetVarTemplate>

<mt:Ignore>テンプレート発動</mt:Ignore>
<mt:Var name="next" />
<mt:Var name="prev" />

アイテムに登録した画像を一覧表示させる

<mt:Assets type="image" tags="[@タグ]" sort_by="description" sort_order="ascend">
<li><mt:If tag="cf_EntryDataURL"><a href="<mt:cf_url>"<mt:If tag="cf_blank"> target="_blank"</mt:If>></mt:If><img src="<mt:AssetThumbnailURL width="320" />" alt="<mt:AssetDescription remove_html="1" />" /><mt:If tag="cf_EntryDataURL"></a></mt:If></li>
</mt:Assets>

(アイテムの説明欄に<01>等と記入しておくことで順番に並べることができる)

改行を削除

<mt:Unless name="compress" regex_replace="/^\s*\n/gm","">
//ここにコンテンツ
</mt:Unless>

カスタムフィールドでエントリーの並び順を決める

カスタムフィールド設定(任意)

  • タイトル:並び順
  • 説明文:数字2桁で入力。小さい順に並びます。例) 02, 11
  • カスタムフィールドの名前:cf_sort

テンプレートタグ

<mt:Entries sort_by="field:[カスタムフィールドの名前]" sort_order="ascend">

アイテムのアップロード先の変更

http://nplll.com/archives/2011/12/movabletype5mt5.php

  1. mt/tmpl/cms/include/assetupload.tmplを下記に複製する
    mt/alt-tmpl/cms/include/asset
    upload.tmpl

  2. ファイル最上部に下記コードを記述

<mt:setvarblock name="extra_path">assets/<mt:Date format="%Y/%m" /></mt:setvarblock>

アーカイブマッピングで、特定のカテゴリだけ出力する

<mt:CategoryID setvar="myCatID" /><mt:If name="myCatID" like="^(10|11|12|14|16)$">%-C.html</mt:If>

ブロックタグ内で使用できる予約変数(ループ処理で)

http://www.movabletype.jp/documentation/mt5/design/templates/loopvariable.html

name="__first__"
name="__last__"
name="__odd__"
name="__even__"
name="__index__"
name="__counter__"

ブロックタグ内で要素を3回に一度出現させる

<mt:Entries>
<mt:If name="__counter__" op="%" value="3" eq="0">3回目に出現して3回ごとに繰り返す</mt:If>
</mt:Entries>

ブロックタグ内で特定のタイミングで要素を区切る

<mt:Entries>
<mt:If name="__first__"><ul></mt:If>
<li><mt:EntryTitle /></li>
<mt:If name="__last__"></ul><mt:ElseIf name="__counter__" op="%" value="4" eq="0"></ul><ul></mt:If>
</mt:Entries>

→出力

<ul>
<li>aaa</li>
<li>aaa</li>
<li>aaa</li>
<li>aaa</li>
</ul>
<ul>
<li>aaa</li>
<li>aaa</li>
...
...
...
</ul>

jsonをMTMLで処理(MTappjQuery使用)

<mt:EntryDataJson convert_breaks="0" json_decode="1" setvar="json" />
<mt:Var name="json" key="items" setvar="items">
<mt:loop name="items">
<mt:If name="__first__"><table></mt:If>
<tr>
<th><mt:Var name="key1"></th>
<td><mt:Var name="key2"></td>
</tr>
<mt:If name="__last__"></table></mt:If>
</mt:loop>
<mt:SetVar name="items">

フォルダをベースとしたウェブページに使えるパンくずリスト

<div class="c-topicpath">
    <ol class="c-topicpath-lists">
        <li class="c-topicpath-lists__item"><a href="<mt:Var name="website_url" />">TOP</a></li>
<mt:PageFolder><mt:FolderLabel setvar="page_folder" /></mt:PageFolder>
<mt:ParentFolders>
    <mt:If tag="FolderLabel" eq="$page_folder">
        <mt:If tag="PageBasename" eq="index">
        <li class="c-topicpath-lists__item"><mt:PageTitle /></li>
        <mt:Else>
        <li class="c-topicpath-lists__item"><a href="<mt:Var name="website_url" /><mt:FolderPath />/"><mt:FolderLabel /></a></li>
        <li class="c-topicpath-lists__item"><mt:PageTitle /></li>
        </mt:If>
    <mt:Else>
        <li class="c-topicpath-lists__item"><a href="<mt:Var name="website_url" /><mt:FolderPath />/"><mt:FolderLabel /></a></li>
    </mt:If>
</mt:ParentFolders>
    </ol>
<!-- .c-topicpath --></div>

MTタグ以外

アイテムのアップロード先の変更

mt/tmpl/cms/include/asset_upload.tmplを下記に複製する

mt/alt-tmpl/cms/include/asset_upload.tmpl

ファイル最上部に下記コードを記述

<mt:setvarblock name="extra_path">assets/<$mt:date format="%Y/%m"$></mt:setvarblock>

セキュリティ対策

http://www.movabletype.jp/guide/movable-type-security-guide.html

バックアップの保存先の変更

http://www.movabletype.jp/faq/change-destination-of-backup.html