Nucleusのif文を拡張しよう【修正】
Nucleusの標準のif文はいささか不便で、それを補うプラグインも多く出ています。
Nucleusのif文でカテゴリを選択する際に複数のカテゴリのいずれかに(属する・属さない)という判別を必要とする場合があります。
このような場合、標準のif文では下記のようにするしかありません。
例えば下記ではカテゴリは1,3,5のいずれでもないときは「表示したい内容」を表示するというものですが、この場合でも下記のようにif文を3つ書く必要があり、スキンの編集画面が見にくくなります。
<%if(category,catid,1)%>表示したい内容<%endif%>
<%if(category,catid,3)%>表示したい内容<%endif%>
<%if(category,catid,5)%>表示したい内容<%endif%>
これを少し便利にするために /nucleus/libs/ACTIONS.php の function _ifCategory() の改造をします。 function _ifCategory()のコードを下記に置き換えます。
(※この改造はNucleusのコアファイルの改造になりますので、作業にはご注意ください。Ver.3.31SP3で動作の確認をしていますが、この改造で万が一被害を被られましても一切の保証はいたしませんので予めご了承ください。)
※以前掲載したものがおかしかったので修正しました
global $blog, $catid;
$pattern= '/^'.$catid.'\||\|'.$catid.'\||\|'.$catid.'$/';
if (($name == 'catid') && ($catid != "")){
if (preg_match($pattern,$value)=='1') {
return $blog->isValidCategory($catid);
}
}
// when no parameter is defined, just check if a category is selected
if (($name != 'catname' && $name != 'catid') || ($value == ''))
return $blog->isValidCategory($catid);
// check category name
if ($name == 'catname') {
$value = $blog->getCategoryIdFromName($value);
if ($value == $catid)
return $blog->isValidCategory($catid);
}
// check category id
if (($name == 'catid') && ($value == $catid))
return $blog->isValidCategory($catid);
return false;
}
これを行なうことでif,ifnotを下記のように拡張します。 先ほどの例のif文を下記のように短縮できて見やすくなります。if文を複数並べる際に便利です。
<%ifnot(category,catid,1|3|5)%> 表示したい内容 <%endif%>
↓は以前掲載していましたが、正常な動作をしなかったので使用しないでください。
global $blog, $catid;
// when no parameter is defined, just check if a category is selected
if (($name != 'catname' && $name != 'catid') || ($value == ''))
return $blog->isValidCategory($catid);
// check category name
if ($name == 'catname') {
$value = $blog->getCategoryIdFromName($value);
if ($value == $catid)
return $blog->isValidCategory($catid);
}
// check category id
if (($name == 'catid') && ($value == $catid))
return $blog->isValidCategory($catid);
$pattern= '/^'.$catid.'\||\|'.$catid.'\||\|'.$catid.'$/';
if (($name == 'catid') && (preg_match($pattern,$value)))
return $blog->isValidCategory($catid);
return false;
}
http://jbar.jp/action.php?action=plugin&name=TrackBack&tb_id=942













