静的出力と動的出力併用時のページネーション
MovableType.org – MT5 Documentation: Paginating the Main Index Templateを参考にした。メインインデックスだけでなく、アーカイブでも利用可能。
GetEntriesOnIndexプラグインを利用すれば、1ページ当たりのエントリー数を動的に取得できる。
ページネーションMTML
<mt:Ignore>1ページあたりのエントリー数…どちらかを選択</mt:Ignore> <mt:Ignore><$mt:SetVar name="entries_per_page" value="10"$></mt:Ignore> <mt:Ignore>1ページあたりのエントリー数を自作プラグインで「表示される記事数」を取得 https://www.anothersky.pw/skyward/archives/000131.html</mt:Ignore> <mt:If name="entries_per_page" eq=""> <$mt:EntriesOnIndex setvar="entries_per_page"$> </mt:If> <mt:Ignore>2ページ目以降を出力するスクリプトのパスなど</mt:Ignore> <mt:SetVarBlock name="search_link"> <$mt:CGIRelativeURL$><$mt:SearchScript$>?IncludeBlogs=<$mt:BlogID$> &template_id=<$mt:BuildTemplateID$> &limit=<$mt:Var name="entries_per_page"$> <mt:If name="main_index"> &archive_type=Index <mt:Else> &archive_type=<$mt:ArchiveType$> </mt:If> <mt:Ignore><カテゴリ-年別などの場合は予約変数名を変更する</mt:Ignore> <mt:If name="category_archive"> &category=<$mt:CategoryID$> </mt:If> &page= </mt:SetVarBlock> <mt:Ignore>スクリプトのパスをクリーンアップ</mt:Ignore> <$mt:GetVar name="search_link" strip="" trim="1" setvar="search_link"$> <mt:Ignore>総エントリー数集計(全エントリー数 or アーカイブ毎のエントリー数)</mt:Ignore> <mt:If name="archive_template"> <$mt:ArchiveCount setvar="total_entry_count"$> <mt:Else> <$mt:BlogEntryCount setvar="total_entry_count"$> </mt:If> <mt:Ignore>総エントリー数が1ページあたりのエントリー数を上回っていたら、ナビゲーションを表示</mt:Ignore> <mt:If name="total_entry_count" gt="$entries_per_page"> <mt:If name="search_results"> <mt:Ignore>mt-search用</mt:Ignore> <$mt:CurrentPage setvar="cur_page"$> <ul> <mt:IfPreviousResults> <li><a href="<$mt:PreviousLink$>" rel="prev">< 前へ</a></li> </mt:IfPreviousResults> <mt:IfMoreResults> <li><a href="<$mt:NextLink$>" rel="next">次へ ></a></li> </mt:IfMoreResults> </ul> <ol> <mt:PagerBlock> <mt:If name="__value__" eq="$cur_page"> <li><em><$mt:GetVar name="__value__"$></em></li> <mt:Else> <li><a href="<$mt:PagerLink$>"><$mt:GetVar name="__value__"$></a></li> </mt:If> </mt:PagerBlock> </ol> <mt:Else> <mt:Ignore>スタティックページ用</mt:Ignore> <$mt:GetVar name="total_entry_count" setvar="page_count"$> <mt:Ignore>e.g.) 52</mt:Ignore> <$mt:SetVar name="page_count" op="--"$> <mt:Ignore>e.g.) 51</mt:Ignore> <$mt:SetVar name="modulo" value="$page_count"$> <mt:Ignore>e.g.) 51</mt:Ignore> <$mt:SetVar name="modulo" op="%" value="$entries_per_page"$> <mt:Ignore>e.g.) 1</mt:Ignore> <$mt:SetVar name="page_count" op="-" value="$modulo"$> <mt:Ignore>e.g.) 51</mt:Ignore> <$mt:SetVar name="page_count" op="/" value="$entries_per_page"$> <mt:Ignore>e.g.) 5</mt:Ignore> <$mt:SetVar name="page_count" op="++"$> <mt:Ignore>e.g.) 6</mt:Ignore> <ul> <li><a href="<$mt:GetVar name="search_link">2" rel="next">次へ ></a></li> </ul> <ol> <mt:For var="page_no" from="1" to="$page_count"> <mt:If name="__first__"> <li><em>1</em></li> <mt:Else> <li><a href="<$mt:GetVar name="search_link"><$mt:GetVar name="page_no"$>"><$mt:GetVar name="page_no"$></a></li> </mt:If> </mt:For> </ol> </mt:If> </mt:If>
記事表示MTML
下記のようにmt:Entriesにモディファイアの設定が必要。
<mt:Ignore><$mt:SetVar name="entries_per_page" value="10"$></mt:Ignore> <$mt:EntriesOnIndex setvar="entries_per_page"$> <mt:Entries limit="$entries_per_page" search_results="1"> <!-- Do something --> </mt:Entries>
備考
「テンプレートはインデックスではないアーカイブタイプのブログ記事リストでなければなりません」のエラーが出た場合は、データベースの操作が必要。(「カテゴリ別記事リスト」テンプレートのページ送りエラーの対処法 - eigerで作るMovableType)