WordPress2.6でトラックバックとコメントをわける方法 その2

Get Recent Commentsというこのプラグインを使用するとサイドバーにコメントとトラックバックを分けて表示できる。大変便利なプラグインなのですが、そのままでは文字化けが発生したり、ウィジェットの更新時にタイトルがおかしくなるなどの問題がある。

文字化け対策

1487行目あたりにある下記のkjgrc_excerptという関数を以下のように変更する。
function kjgrc_excerpt ($text,$chars_per_comment,$chars_per_word,$tag,$output)
{
$length = strlen(str_replace($tag,"",strip_tags($output)));
$length = $chars_per_comment - $length;
$length = $length -2; // we will add three dots at the end
if ($length <0) $length = 0;
if (strlen($text)> $length) {
if (function_exists('mb_strimwidth')) {
$text = mb_strimwidth($text, 0, $chars_per_comment, '...', 'utf-8');
} else {
$text = substr($text,0,$length);
$text = substr($text,0,strrpos($text,' '));
// last word exceeds max word length:
if ((strlen($text) - strrpos($text,' '))> $chars_per_word) {
$text = substr($text,0,strlen($text)-3);
}
$text = $text . "...";
}
}
#$text = "[EXCERPT]: '$text'";
return "$text";
}

ウィジェットの更新時にタイトル化け対策

691行目あたりにあるkjgrc_use_cacheという関数を以下のように変更する。
function kjgrc_use_cache()
{
if( strstr($_SERVER['REQUEST_URI'], 'wp-admin/widgets.php') ) return 0;
add_option('kjgrc_use_cache',1);
return get_option("kjgrc_use_cache");
}
【参考にした記事】
Recent Commentsの修正
サイドバーのコメント表示プラグイン Get Recent Comments

追記:2011/04/16
WordPress2.8以降+Get Recent Comments 2.0.6以降
の組み合わせの場合、修正等はいりません。
追記:2011/06/06
WordPress3.1でトラックバックとコメントをわける方法(サイドバー対応込)|某氏の猫空

スポンサーリンク