TopMovable Typeプラグイン > ブログ記事に同一カテゴリーのブログ記事の前後リンクを表示する(Movable Type 4.2対応)
News
各種ブログテンプレート
2008年11月18日

エントリー本文

ブログ記事に同一カテゴリーのブログ記事の前後リンクを表示する(Movable Type 4.2対応)

Posted at November 18,2008 12:55 AM
Category:[エントリー, カテゴリー, プラグイン]
Tag:[, , , ]

ブログ記事に同一カテゴリーのブログ記事の前後リンクを表示する」の Movable Type 4.2 版のプラグインを公開します。

元記事の内容は Movable Type 4.1 の内容であり、Movable Type 4.2 では前後リンクが正常に表示されない場合があります。

1.プラグインの概要

下の画面(元記事の再掲)のブログ記事タイトル上にある前後リンクは、分かりにくいですが、同一カテゴリーのブログ記事です。

また、ブログ記事に複数カテゴリーを設定している場合は、すべてのカテゴリーの前後リンクも表示します。

ブログ記事に同一カテゴリーのブログ記事の前後リンクを表示する」では、元のプラグイン「Previous and next in category」を修正する形で書いてましたが、修正箇所が多いため、プラグインファイルで公開します(ファイル内に元プラグインの著者を掲載しています)。

このプラグインは Movable Type 4.1 / 4.2 で動作することを確認しています。

2.PreviousNextInCategory プラグインのダウンロード

下記の PreviousNextInCategory.zip をクリックして、プラグインアーカイブをダウンロード。

PreviousNextInCategory.zip

3.PreviousNextInCategory プラグインのアップロード・インストール

プラグインアーカイブを展開し、中にある PreviousNextInCategory フォルダごと、Movable Type のアプリケーションディレクトリの plugins ディレクトリにアップロード。

システム管理画面のプラグイン一覧で、「PreviousNextInCategory ~」が表示されればインストール完了です。

プラグイン一覧

4.プライマリカテゴリーに属するブログ記事のみを表示する場合

ブログ記事アーカイブの任意の位置に下記のサブテンプレートを設定してください。

<MTEntryPreviousInCategory>
<a href="<$MTEntryPermalink$>">« <$MTEntryTitle$></a> | 
</MTEntryPreviousInCategory>
<a href="<$MTEntryPermalink archive_type="Category"$>"><$MTEntryCategory$></a>
<MTEntryNextInCategory>
 | <a href="<$MTEntryPermalink$>"><$MTEntryTitle$> »</a>
</MTEntryNextInCategory>

ブログ記事アーカイブを再構築すれば、冒頭のように、同一カテゴリーの前後記事リンクが表示されます。

5.ブログ記事に登録されているすべてのカテゴリーに属するブログ記事を表示する場合

ブログ記事アーカイブの任意の位置に、下記のサブテンプレートを設定してください。

<MTEntryCategories>
<p>
<MTEntryPreviousInCategory>
<a href="<$MTEntryPermalink$>">« <$MTEntryTitle$></a> | 
</MTEntryPreviousInCategory>
<a href="<$MTCategoryArchiveLink$>"><$MTCategoryLabel$></a>
<MTEntryNextInCategory>
 | <a href="<$MTEntryPermalink$>"><$MTEntryTitle$> »</a>
</MTEntryNextInCategory>
</p>
</MTEntryCategories>

ブログ記事アーカイブを再構築すれば、ブログ記事が属する全カテゴリーの前後記事リンクが表示されます。

6.元プラグインの変更点

一応、元プラグイン PreviousNextInCategory.pl の変更点を掲載しておきます。実際にはこの修正に加えて、ブログ記事が再構築されたときに同一カテゴリーの前後記事を再構築する処理も追加しています。

ブログ記事に同一カテゴリーのブログ記事の前後リンクを表示する」での変更(青色部分を追加)

sub _hdlr_entry_previous_in_category {
    my($ctx, $args, $cond) = @_;
    my $e = $ctx->stash('entry')
        or return $ctx->_no_entry_error('MTEntryPrevious');
    my $cat = $e->category
            or return '';
    if($ctx->stash('category') ne '') {
        $cat = $ctx->stash('category');
    }
    my $prev = $e->previous(1);
    my $res = '';
        while ($prev && !$prev->is_in_category($cat)){
            $prev = $prev->previous(1);
    }
    if ($prev) {
        my $builder = $ctx->stash('builder');
        local $ctx->{__stash}->{entry} = $prev;
        local $ctx->{current_timestamp} = $prev->created_on;
        my %cond = %$cond;
        $cond{EntryIfAllowComments} = $prev->allow_comments;
        $cond{EntryIfCommentsOpen} = $prev->allow_comments eq '1';
        $cond{EntryIfAllowPings} = $prev->allow_pings;
        $cond{EntryIfExtended} = $prev->text_more ? 1 : 0;
        my $out = $builder->build($ctx, $ctx->stash('tokens'), \%cond);
        return $ctx->error( $builder->errstr ) unless defined $out;
        $res .= $out;
    }
    $res;
}
sub _hdlr_entry_next_in_category {
    my($ctx, $args, $cond) = @_;
    my $e = $ctx->stash('entry')
        or return $ctx->_no_entry_error('MTEntryNext');
    my $cat = $e->category
            or return '';
    if($ctx->stash('category') ne '') {
        $cat = $ctx->stash('category');
    }
    my $next = $e->next(1);
    my $res = '';
        while ($next && !$next->is_in_category($cat)){
            $next = $next->next(1);
    }
    if ($next) {
        my $builder = $ctx->stash('builder');
        local $ctx->{__stash}->{entry} = $next;
        local $ctx->{current_timestamp} = $next->created_on;
        my %cond = %$cond;
        $cond{EntryIfAllowComments} = $next->allow_comments;
        $cond{EntryIfCommentsOpen} = $next->allow_comments eq '1';
        $cond{EntryIfAllowPings} = $next->allow_pings;
        $cond{EntryIfExtended} = $next->text_more ? 1 : 0;
        my $out = $builder->build($ctx, $ctx->stash('tokens'), \%cond);
        return $ctx->error( $builder->errstr ) unless defined $out;
        $res .= $out;
    }
    $res;
}

4.2 対応の変更(赤色が 4.1 までの追加部分で、青色部分をさらに追加)

sub _hdlr_entry_previous_in_category {
    my($ctx, $args, $cond) = @_;
    my $e = $ctx->stash('entry')
        or return $ctx->_no_entry_error('MTEntryPrevious');
    my $cat = $e->category
            or return '';
    if($ctx->stash('category') ne '') {
        $cat = $ctx->stash('category');
    }
    my $terms;
    $terms->{category_id} = $cat->id;
    my $prev = $e->previous(1, $terms);
    my $res = '';
        while ($prev && !$prev->is_in_category($cat)){
            $terms->{category_id} = $cat->id;
            $prev = $prev->previous(1, $terms);
    }
    if ($prev) {
        my $builder = $ctx->stash('builder');
        local $ctx->{__stash}->{entry} = $prev;
        local $ctx->{current_timestamp} = $prev->created_on;
        my %cond = %$cond;
        $cond{EntryIfAllowComments} = $prev->allow_comments;
        $cond{EntryIfCommentsOpen} = $prev->allow_comments eq '1';
        $cond{EntryIfAllowPings} = $prev->allow_pings;
        $cond{EntryIfExtended} = $prev->text_more ? 1 : 0;
        my $out = $builder->build($ctx, $ctx->stash('tokens'), \%cond);
        return $ctx->error( $builder->errstr ) unless defined $out;
        $res .= $out;
    }
    $res;
}
sub _hdlr_entry_next_in_category {
    my($ctx, $args, $cond) = @_;
    my $e = $ctx->stash('entry')
        or return $ctx->_no_entry_error('MTEntryNext');
    my $cat = $e->category
            or return '';
    if($ctx->stash('category') ne '') {
        $cat = $ctx->stash('category');
    }
    my $terms;
    $terms->{category_id} = $cat->id;
    my $next = $e->next(1, $terms);
    my $res = '';
        while ($next && !$next->is_in_category($cat)){
            $terms->{category_id} = $cat->id;
            $next = $next->next(1, $terms);
    }
 
    if ($next) {
        my $builder = $ctx->stash('builder');
        local $ctx->{__stash}->{entry} = $next;
        local $ctx->{current_timestamp} = $next->created_on;
        my %cond = %$cond;
        $cond{EntryIfAllowComments} = $next->allow_comments;
        $cond{EntryIfCommentsOpen} = $next->allow_comments eq '1';
        $cond{EntryIfAllowPings} = $next->allow_pings;
        $cond{EntryIfExtended} = $next->text_more ? 1 : 0;
        my $out = $builder->build($ctx, $ctx->stash('tokens'), \%cond);
        return $ctx->error( $builder->errstr ) unless defined $out;
        $res .= $out;
    }
    $res;
}
Posted by yujiro
この記事を読んだ人はこんな記事も読んでいます
関連記事
人気エントリー
Hatena Hot Entries
Hatena Entries
トラックバックURL


トラックバック

MTEntryNext,Previousの拡張(とか改良?とか)。 from Junnama Online (Mirror)
# 何回も書いてる気がするなぁ、これ。 小粋空間: ブログ記事に同一カテゴリーのブログ記事の前後リンクを表示する(Movable Type 4.2対応) ... [続きを読む]

Tracked on November 19, 2008 5:25 PM

ブログ記事に同一カテゴリーの前後リンクを表示する from Ubuntuでマルチメディア
小粋空間さんの「ブログ記事に同一カテゴリーのブログ記事の前後リンクを表示する」を... [続きを読む]

Tracked on January 8, 2009 11:15 PM
コメントする
greeting

*必須



ご質問のコメントの回答については、内容あるいは多忙の場合、1週間以上かかる場合があります。また、すべてのご質問にはお答えできない可能性があります。予めご了承ください。

太字 イタリック アンダーライン ハイパーリンク 引用
[サインインしない場合はここにCAPTCHAを表示します]

コメント投稿後にScript Errorや500エラーが表示された場合は、すぐに再送信せず、ブラウザの「戻る」ボタンで一旦エントリーのページに戻り(プレビュー画面で投稿した場合は、投稿内容をマウスコピーしてからエントリーのページに戻り)、ブラウザをリロードして投稿コメントが反映されていることを確認してください。

コメント欄に(X)HTMLタグやMTタグを記述される場合、「<」は「&lt;」、「>」は「&gt;」と入力してください。例えば「<$MTBlogURL$>」は「&lt;$MTBlogURL$&gt;」となります(全て半角文字)

Now loading...
Introduction
Entry Trackbacks
MTEntryNext,Previousの拡張(とか改良?とか)。
 [Junnama Online (Mirror)] 11/19 17:25
ブログ記事に同一カテゴリーの前後リンクを表示する
 [Ubuntuでマルチメディア] 01/08 23:15
Entries of this Category
Recent Entries
Recent Comments
Recent Trackbacks
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 ...
Categories
Monthly Archives
BlogPeople
Now loading...
Syndicate this site
FeedBurner(RSS1.0/RSS2.0/Atom)
Counter
これまでのアクセス
クリエイティブ・コモンズ・ライセンス
Powered by
Movable Type 4.261
 
List Me!