
【Movable Type】管理画面の記事一覧に親カテゴリを表示するプラグイン
ブログ記事の一覧画面で親カテゴリがあった場合に表示するプラグインをつくりました。

メインカテゴリの親カテゴリーが存在すればブログ記事一覧に表示します。(存在しなければ空欄)
プラグインディレクトリにアップし、[設定]-[プラグイン]にて「プラグインを有効にする」に
チェックを入れて有効にしてご使用ください。
リポジトリはこちら↓
https://github.com/tam-yamaguchi/mt-plugin-displayparentcategory
ダウンロードファイルはこちら↓
DisplayParentCategory.zip
MTオブジェクトを扱うのは初めてで、かなり試行錯誤しました。
リストプロパティ部分を抜粋するとこんな感じ。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | require MT::Placement; require MT::Category; my %parentHash ; for my $category (MT::Category->load( { blog_id => $blog_id } ) ){ if ( $category ->parent_category == undef ){ $parentHash { $category ->id} = "" ; } else { $parentHash { $category ->id} = $category ->parent_category->label; } } $plugin ->registry({ list_properties => { entry => { parentname => { label => 'ParentCategory' , display => 'default' , order => 500, raw => sub { my $prop = shift ; my ( $objs , $app , $opts ) = @_ ; my $parent_name ; my @placements = MT::Placement->load({ entry_id => $objs ->id, is_primary => 1 }); for my $placement ( @placements ){ $parent_name = $parentHash { $placement ->category_id}; } return $parent_name ; }, bulk_sort => sub { my $prop = shift ; my ( $objs ) = @_ ; return sort { $prop ->raw( $a ) cmp $prop ->raw( $b ) } @$objs ; }, }, }, } }); |
親カテゴリ名はあらかじめハッシュでもたせています。
ソートが遅いです。。。
ドキュメントにもパフォーマンス劣化の恐れがあると記載されているので、改善は難しいのかなあ?
https://github.com/movabletype/Documentation/wiki/Ja-dev-registry-list-properties
リポジトリはこちら↓
https://github.com/tam-yamaguchi/mt-plugin-displayparentcategory
ダウンロードファイルはこちら↓
DisplayParentCategory.zip