株式会社マイティーエース- MightyAce Inc.

資料請求

お問い合わせ

運用型広告

Google・Yahoo!のリスティング広告はもちろん、Facebook・Instagram・LINE・XなどのSNS広告も運用が可能です。

マンガ制作

WEBマンガを中心に、LP・バナー・SNSなど、お客様の商品(サービス)の魅力を伝える完全オリジナルのマンガを制作いたします。

Webページ制作

ページに訪れるユーザーの目的と興味を引く、お客様一人一人のニーズに合わせた完全オリジナルのWebコンテンツを制作いたします。

アクセス解析

精度を高めつつ分析⇒改善のサイクルを回し続け、意思決定に繋がるレポーティングで売上最大化のお手伝いをいたします。

Web制作

WordPressのランキングをプラグインなしでつくる

今回は、WordPressで表示回数が多い順に投稿を表示させる方法をご紹介します。

ランキング表示にはWordPress Popular Postsプラグインが有名です。

それをインストールしてしまえば手っ取り早いのですがWordPressプラグインは出来るかぎり少ない方が望ましいので、直接コードを書いていきます。

 

functions.phpを編集する

// 人気記事出力用関数
function getPostViews($postID){
$count_key = 'post_views_count';
$count = get_post_meta($postID, $count_key, true);
if($count==''){
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, '0');
return "0 View";
}
return $count.' Views';
}
// 記事viewカウント用関数
function setPostViews($postID) {
$count_key = 'post_views_count';
$count = get_post_meta($postID, $count_key, true);
if($count==''){
$count = 0;
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, '0');
}else{
$count++;
update_post_meta($postID, $count_key, $count);
}
}
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);

 

人気記事一覧を出力する

人気記事一覧を出力したい部分に以下のコードをコピペ。

<?php
// views post metaで記事のPV情報を取得する
setPostViews(get_the_ID());


$args = array(
'meta_key' => 'post_views_count',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'posts_per_page' => 5 // ← 5件取得
);


// WP_Queryによるループ
$query = new WP_Query($args);
if ($query->have_posts()) :
while ($query->have_posts()) :
$query->the_post();
?>

<!-- サムネイルの表示 タイトルの表示 -->
<div>
<a href="<?php the_permalink(); ?>">
<?php if ( has_post_thumbnail() ) { the_post_thumbnail( 'post-thumbnail'); } ?>
<p>
<?php the_title(); ?>
</p>
<?php echo getPostViews(get_the_ID()); // 記事閲覧回数表示 ?>
</div>

<?php
endwhile;
endif;
wp_reset_postdata();
?>

以上になります。
よいWordPressライフを!

Request Materials資料請求

資料請求

Webマーケティングにおけるパフォーマンス改善のための資料がダウンロードできます。

  • web広告の改善事例が知りたい方
  • 現在お願いしている代理店との比較をしたい方
  • サービスの全体像・料金を知りたい方