運用型広告
Google・Yahoo!のリスティング広告はもちろん、Facebook・Instagram・LINE・XなどのSNS広告も運用が可能です。
Web制作
今回は、WordPressで表示回数が多い順に投稿を表示させる方法をご紹介します。
ランキング表示にはWordPress Popular Postsプラグインが有名です。
それをインストールしてしまえば手っ取り早いのですがWordPressプラグインは出来るかぎり少ない方が望ましいので、直接コードを書いていきます。
// 人気記事出力用関数
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ライフを!
Webマーケティングにおけるパフォーマンス改善のための資料がダウンロードできます。