Jun
06
2009
8

WP-PostViewsの抜粋文字数削減

14:18 pm

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になります。

Mar
04
2009
4

WP-PostViewsプラグイン(追記)

20:35 pm

とりあえず動くことは動いてるけど、
・管理画面でページのカウント等を非表示にするoptionをセットしても反映しない
・index.phpでは表示されない
・Multibyte_Excerpt プラグインで表示文字数を変えたのに反映しない
等の不具合が生じています。
時間がなくてちゃんと検証してないので、一部(特に2番目)は自分のミスの可能性もあります。
週末にちゃんとチェックしてみます。

Written by hbirds in: WPカスタマイズ | Tags: ,
Mar
02
2009
0

WP-PostViewsプラグイン

20:48 pm

個別記事の閲覧数をカウントして、ランキングリストを作ってくれるプラグインです。
インストールは通常のプラグインと同様です。
WP-PostViews と WP-PostViewsWidget の二つのプラグインができていますが、WP-PostViewsWidget を有効化するとウィジェットにMost Viewedというウィジェットが追加されますが、サイドバーにMost Viewed を表示したくない場合は、後者の方は有効化しなくてもかまいません。

これだけでは、カウントはしてくれても表示してくれていないので、index.php などの個別記事を表示するテンプレートの適当な位置に以下のスクリプトを追記します。

<?php if(function_exists('the_views')) { the_views(); } ?>

また、このサイトでは、トップページの下の方に人気の記事として、トップ10件(デフォルト)の記事タイトルを載せていますが、以下のスクリプトをtop_page.php に追記しています。

<?php if (function_exists('get_most_viewed')): ?>
<?php get_most_viewed(); ?>
<?php endif; ?>

↑のままだと、ページも含めて閲覧回数が表示されてしまうので、記事に限定して、かつ上位5件のリスト表示をするようにしました。

    <li><h2>人気の記事リスト</h2>
    <ul>
    <?php if (function_exists('get_most_viewed')): ?>
    <?php get_most_viewed('post',5); ?>
    <?php endif; ?>
    </ul></li>
Written by hbirds in: WPカスタマイズ | Tags: ,

www.hbirds.net