wordpressで人気の記事をサイドバーに表示する。

WP-PostViews というプラグインを利用。

WP-PostViews のインストールは、
プラグイン > 新規追加 画面で検索し、選択。画面の指示に従って完了。プラグインを有効化することを忘れないように。

実際の表示は、
外観 > ウィジェット 画面で設定する。

表示順位を掲載するには、プログラム自体の改変が必要。
\wp-content\plugins\wp-postviews\wp-postview.php

foreach ($most_viewed as $post) {
$post_views = intval($post->views);
$post_title = get_the_title();
if($chars > 0) {
$post_title = snippet_text($post_title, $chars);
}
$post_excerpt = views_post_excerpt($post->post_excerpt, $post->post_content, $post->post_password, $chars);
$post_content = get_the_content();
$temp = stripslashes($views_options[‘most_viewed_template’]);
$temp = str_replace(“%VIEW_COUNT%”, number_format_i18n($post_views), $temp);
$temp = str_replace(“%POST_TITLE%”, $post_title, $temp);
$temp = str_replace(“%POST_EXCERPT%”, $post_excerpt, $temp);
$temp = str_replace(“%POST_CONTENT%”, $post_content, $temp);
$temp = str_replace(“%POST_URL%”, get_permalink(), $temp);
$output .= $temp;
}

この記述があるのは複数個所ある。

238、287、341、395、449、503

$rankingNo = 1;//順位を計数する変数の定義
foreach ($most_viewed as $post) {
$post_views = intval($post->views);
$post_title = get_the_title();
if($chars > 0) {
$post_title = snippet_text($post_title, $chars);
}
$post_excerpt = views_post_excerpt($post->post_excerpt, $post->post_content, $post->post_password, $chars);
$post_content = get_the_content();
$temp = stripslashes($views_options[‘most_viewed_template’]);
$temp = str_replace(“%VIEW_COUNT%”, number_format_i18n($post_views), $temp);
$temp = str_replace(“%POST_TITLE%”, $post_title, $temp);
$temp = str_replace(“%POST_EXCERPT%”, $post_excerpt, $temp);
$temp = str_replace(“%POST_CONTENT%”, $post_content, $temp);
$temp = str_replace(“%POST_URL%”, get_permalink(), $temp);
$temp = str_replace(“%RANKING_NO%”, $rankingNo, $temp);//変数への代入
$output .= $temp;
$rankingNo++;//順位のカウントアップ
}

に変更する。変更するのはコメントのあるところ。コードに追加する。それ以外のコードは、微妙に違うのでまるッと上書きしないように。