機能

ポストした記事やカテゴリなどの閲覧数を簡単に確認するプラグイン

導入

プラグインの追加から、wp-postviews を選択してインストール。

表示

カウント結果は管理画面ですぐ確認できます。

もし、投稿したページなどで確認したい場合は、例えば記事ページなら、single.php に以下のコードを埋め込みましょう。

単純にそのページのVIEW数だけを表示

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

よく見られるページの一覧表示

以下のコードの場合、投稿(post)の上位10件

<?php if(function_exists('get_most_viewed')): ?>
<ul>
  <?php get_most_viewed('post',10); ?>
</ul>
<?php } ?>

起こった問題

warning number_format

Warning: number_format() expects parameter 1 to be float, string given in /var/www/gctrip/html/wp-includes/functions.php on line 265

これは、まだ1度もカウントされていないページがあった場合に表示されてしまうplugin のバグでした。

編集ファイル:/wp-content/plugins/wp-postviews/wp-postviews.php

333行目
$temp = str_replace( '%VIEW_COUNT%', $post_views?number_format_i18n( $post_views ):0, $temp );
334行目
$temp = str_replace( '%VIEW_COUNT_ROUNDED%', $post_views?postviews_round_number( $post_views ):0, $temp 

おまけ

カウントのリセット方法

カウントをリセットや手動で変更したい場合はDBを修正する必要があります。

table : wp_postmeta

meta_key : views

post_id : 対象の記事、カテゴリなどの post_id

を確認して、meta_value を書き換える事で修正可能。
私の場合、テストで再読込で無駄に回数を付けてしまったのでリセットに使用しました。

この記事は役立ちましたか?
  • はい 
  • いいえ 
カテゴリー: wordpress

712 ビュー