垃圾站 WP教程 2种方法实现WordPress博客文章浏览次数统计功能

2种方法实现WordPress博客文章浏览次数统计功能

2种方法实现WordPress博客文章浏览次数统计功能插图

今天垃圾站博客介绍2种方法实现WordPress博客文章浏览次数统计功能:

(1)使用插件:WP-PostViews Plus 可以简便的实现文章浏览次数统计,效果显示为“阅读:** 浏览数”垃圾站博客用的此方法,效果图如下:

2种方法实现WordPress博客文章浏览次数统计功能插图1

(2)代码的实现博客文章浏览次数统计功能

首先、使用FTP登陆网站服务器,查找主题目录下的functions.php文件,在适当位置添加以下代码:

[php]
/* Postviews start */
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 ";
}
return $count;
}
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);
}
}
/* Postviews start end*/
[/php]

其次修改主题的single.php文件,在single.php文件中的endwhile; endif; wp_reset_query(); 循环前添加如下代码:

[php]
<?php setPostViews(get_the_ID());?>
[/php]

最后在你想要显示文章浏览次数统计的地方,可以是index.php、sidebar.php或single.php等文件中添加如下代码即可:

[php]
<?php echo getPostViews(get_the_ID()); ?> 次浏览
[/php]

本文由垃圾站编辑整理,转载请注明原文地址:https://www.lajiz.cn/1512.html

上一篇
下一篇
联系我们

联系我们

返回顶部