■
カテゴリー決め打ちで、そのカテゴリー内のエントリーに対して、投稿されたコメントを取得したい場合、ちょっと厳しいものがあります。
そこで、プラグインを初開発。
「MTCommentsWithCategory」です。
package MT::Plugin::MTCommentsWithCategory; use strict; use MT; use MT::Template::Context; use MT::Plugin; use MT::Comment; use MT::Entry; use MT::Category; # show plugin information to main menu my $plugin = MT::Plugin->new; $plugin->name('MTCommentsWithCategory 0.01'); $plugin->description('Get comments in specified category.'); MT->add_plugin($plugin); # add container tag MT::Template::Context->add_container_tag(CommentsWithCategory => \&commentswithcategory); # main sub commentswithcategory { my ($ctx, $args) = @_; my $tokens = $ctx->stash('tokens'); my $builder = $ctx->stash('builder'); my $res = ''; my @comments = (); my %commentflag = (); my $matched = 0; my $cnt = 0; my ($category, $lastn, $sort_order, $comment, $entry, $entry_id); # get 'category' attribute $category = $args->{'category'} or return $ctx->error('/"category/" attribute not found'); # get 'lastn' attribute $lastn = $args->{'lastn'} or $lastn = 0; # get 'sort_order' attribute $sort_order = $args->{'sort_order'} or $sort_order = 'descend'; # get comments my $iter = MT::Comment->load_iter({ blog_id => $ctx->stash('blog_id') , visible => 1}, { 'sort' => 'created_on', direction => $sort_order } ); # loop each MT::Comment object while (my $comment = $iter->()) { # get entry_id of current comment $entry_id = $comment->entry_id; # skip already listed entry or commented to category next if ($commentflag{$comment->id} || $entry_id == 0); # load commented entry $entry = MT::Entry->load($entry_id) or return $ctx->error($ctx->errstr); # skip not released entry next if ($entry->status != MT::Entry::RELEASE()); # skip not specified category $matched = 0; my $entcategs = $entry->categories; foreach my $entcateg (@$entcategs) { if($entcateg->label eq $category) { $matched = 1; last if(1); } } next if (!$matched); # set comment information $commentflag{$comment->id} = 1; push @comments, $comment; # when listed lastn comments, exit loop $cnt++; last if ($cnt == $lastn); } # backup stash('comment') my $commentbackup = $ctx->stash('comment'); # out comments foreach $comment (@comments) { $ctx->stash('comment', $comment); local $ctx->{current_timestamp} = $comment->created_on; defined(my $out = $builder->build($ctx, $tokens)) or return $ctx->error($ctx->errstr); $res .= $out; } # restore stash('comment') $ctx->stash('comment', $commentbackup); $res; }
これを「MTCommentsWithCategory.pl」などの名前で保存し、pluginsに入れます。
使い方は、「MTComments」とほぼ同じですが、categoryアトリビュートが必須になります。アトリビュートは、lastnとsort_orderが使用できます。sort_orderのデフォルトは'descend'になります。sort_orderに'ascend'を使うと、多分古い順から取り出されます(これはMTCommentsとは違うかも)。
要するに、特定のカテゴリーの新着コメントを引っ張り出すという目的で使います。細かい動作は「MTComments」をハックしていないので、検証してません。
<MTCommentsWithCategory category="hoge" lastn="5"> .... <MTComments>コンテナの中身を記述 .... </MTCommentsWithCategory>
このままでは、あまり使えないのですが、MTTagInvokeあたりと組み合わせると、結構イけます。
需要があるかどうかは別として、ご利用になった場合に不具合が生じても、責任は負えません。自己責任でお願いいたします。