WP-PostViewsの抜粋文字数削減
WP-PostViewsは、記事の閲覧回数やその記事の抜粋を表示してくれる便利なプラグインですが、抜粋される文字数をWPのデフォルトの数より少なくできるようにしてみました。
wp-postviews.phpを以下のように修正します。
1.先頭の著作権関係のコメントの直後28行目あたりに以下の関数を定義します。
### Function: mb_wordraps
function mb_wordwraps($mb_words, $txt) {
$pointer = 0;
$result_text = "";
while ($pointer < mb_strlen($txt)) {
$this_char = mb_substr($txt,$pointer,1);
if (is_singlebytechar($this_char)){
$mb_words++; // Do not count single byte char;
} elseif ($pointer > $mb_words) {
return mb_substr($txt,0,$pointer);
}
$pointer++;
}
return $txt; //Since length not exceeded return whole text
}
2.以下のコードを4か所に挿入します。
if ($mb_exc_words > 0 ) {
$post_excerpt = mb_wordwraps($mb_exc_words,$post_excerpt).'...';
} else {
$post_excerpt = "";
}挿入する位置は、以下のコード(同じコードが4か所にあります)の直後です。
$post_excerpt = views_post_excerpt($post->post_excerpt, $post->post_content, $post->post_password);
※使い方
ウィジェットやテーマ内に以下のように書けば抜粋文字数が20文字になります。
<ul>
<?php if (function_exists('get_most_viewed')): ?>
<?php $mb_exc_words = 20; ?>
<?php get_most_viewed('post',5); ?>
<?php endif; ?>
</ul>$mb_exc_words=0とすれば抜粋文字数が0になります。