MTOSで、カテゴリに画像を割り当てる
カテゴリごとに画像を割り当てます。
正確に言うと、カテゴリの管理画面に、カスタムフィールドを追加します。
カテゴリと対になるもので、テキスト入力出来るものを作ります。ここに画像のファイル名を入力して、テンプレート側で画像が表示できるHTMLタグを設定しておきます。
参照ページはこちらです。(感謝)
Under the Bridge
MT4.1:プラグイン作成におけるDBへのフィールド追加について(カテゴリ編)
具体的には以下です。
<1>プラグイン作成
---------------------------
package MT::Plugin::SubitemCat;
use base 'MT::Plugin';
use strict;
use MT;
use MT::Plugin;
use MT::Template::Context;
use MT::Entry;
# register plugin
my $plugin = __PACKAGE__->new({
name => 'SubitemCat',
key => 'SubitemCat',
version => '1.00',
author_name => 'your name',
description => 'Sub item of Cat',
schema_version => '1.00',
});
MT->add_plugin($plugin);
# initialize registry
sub init_registry
{
my $plugin = shift;
$plugin->registry({
object_types => {
'category' => {
'sort_index_custom' => 'string(255)',
},
},
tags => {
function => {
'CategorySortIndex' => sub{&_hdlr_sort_index_custom;},
},
},
});
}
sub _hdlr_sort_index_custom {
my ($ctx, $args) = @_;
my $tag = 'CategoryIndexAdd';
(my $category = $ctx->stash('category')) || return $ctx->_no_category_error('MT' . $tag);
return $category->sort_index_custom || '';
}
1;
-----------------------------
これで CategorySortIndex というMTタグが存在するようになります。
SubitemCat.plとして、mtフォルダ直下「plugins」ディレクトリに入れます。
任意で「SubitemCat」などの名前のフォルダに入れます。
次に管理画面にカスタムフィールドを表示させるためのファイルを作ります。
mt直下「tmpl」内「cms」内の「edit_category.tmpl」をダウンロード、以下ソースを追加します。
------------------------------
(省略)
<fieldset>
<h3><__trans phrase="Outbound TrackBacks"></h3>
<mtapp:setting
id="ping_urls"
label="<__trans phrase="Trackback URLs">"
hint="<__trans phrase="Enter the URL(s) of the websites that you would like to send a TrackBack to each time you create an entry in this category. (Separate URLs with a carriage return.)">"
help_page="categories"
help_section="categories_urls_to_ping">
<textarea name="ping_urls" id="ping_urls" cols="" rows="" class="full-width short"><mt:var name="ping_urls" escape="html"></textarea>
</mtapp:setting>
</fieldset>
<!-- ここから追加部所 Category Sort Add -->
<fieldset>
<h3>Input Category Sort Index</h3>
<mtapp:setting
id="sort_index_custom"
label="Category Sort Index Input">
<div class="textarea-wrapper">
<input name="sort_index_custom" id="sort_index_custom" class="full-width" maxlength="100" value="<mt:var name="sort_index_custom" escape="html">" class="wide" />
</div>
</mtapp:setting>
</fieldset>
<!-- ここまで追加部所 Category Sort Add -->
<mt:setvarblock name="action_buttons">
<button
type="submit"
accesskey="s"
title="<__trans phrase="Save changes to this category (s)">"
class="save action primary-button"
><__trans phrase="Save Changes"></button>
</mt:setvarblock>
<mt:include name="include/actions_bar.tmpl" bar_position="bottom" hide_pager="1" settings_bar="1">
</form>
</div>
<mt:include name="include/footer.tmpl">
------------------------------
保存したら、mt直下「alt-tmpl」内「cms」内にアップします。そうすると、MTが優先的にそちらを読みに行きます。
管理画面「.../mt/mt.cgi」に再度サインインすると、バージョンアップを知らせる画面が現れると思います。そうしたらまずは成功。
カテゴリの管理画面を見てみると「Category Sort Index Input」というタイトルのカスタムフィールドが新しくあると思います。
テキストも入力できますが、今回は画像のファイル名を入力しておきます。
画像もアイテムやFTPを使ってアップしておきます。
最後にテンプレートのカテゴリ表示に以下のようなHTMLタグを追加します。。
--------------------------------
<mt:Entries>
<mt:IfNonEmpty tag="EntryCategories">
<mt:EntryCategories>
<img src="<$mt:BlogURL$>/img/<$mt:CategorySortIndex$>" alt="<$mt:CategoryLabel$>" />
</mt:EntryCategories>
</mt:IfNonEmpty>
</mt:Entries>
--------------------------------
上記で、指定した画像にカテゴリ名のaltが入って表示されます。
利用方法としては、カテゴリのリスト表示などで個別のアイコン画像を頭につけたりとか、リスト自体を画像で組んだり出来ますね。
長くなってすみません。
July 27, 2011 2:38 PM
|
カテゴリー:
MTOS LOVE!
||
MTタグ記述を覚えとこう!
||
ありがとう!プラグイン
||
カスタムフィールドをカスタマイズ
|
|
コメント(0)
|
トラックバック(0)
|