加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > CMS系统 > wordpress > 正文

WordPress文章刷新不影响浏览量点击量统计代码

发布时间:2020-12-14 14:24:47 所属栏目:wordpress 来源:网络整理
导读:由站长 通过网络收集整理的代码片段。编程之家小编现在分享给大家,也给大家做个参考。 目前 wordpress 系统建站已经非常流行,占据了建设系统的半壁江山,无非是 WordPress 功能强大开发方便扩展性极强,但是 wordpress 程序默认是没有文章

以下代码由PHP站长网 52php.cn收集自互联网现在PHP站长网小编把它分享给大家,仅供参考

目前 wordpress 系统建站已经非常流行,占据了建设系统的半壁江山,无非是 WordPress 功能强大开发方便扩展性极强,但是 wordpress 程序默认是没有文章浏览器统计,这点就很鸡肋。之前我们有给大家分享过,纯代码为 wordpress 添加文章浏览量统计功能,但是无论是用纯代码还是使用 wordpress 插件 wp-postview,当我们刷新当前页面会算作一次浏览数量,那今天创客云给大家介绍下刷新不重复记录的文章浏览量统计代码!

方法一

一、在 wordpress 主题下 functions.php 里增加以下代码:

//add by charleswu

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,'0');

return "0";

}

return $count;

}

function setPostViews($postID) {

$count_key = 'post_views_count';

$count = get_post_meta($postID,true);

if ($count == '') {

$count = 0;

delete_post_meta($postID,'0');

} else {

$count++;

update_post_meta($postID,$count);

}

}

二、解决刷新统计数增加,一定要放在文章页面的最前面,貌似 php 设置 cookie 之前不能有输出,蛋疼。我的是 single.php 页面:

<?php

$post_id=get_the_ID();

if(isset($_COOKIE['views'.$post_id.COOKIEHASH]) && $_COOKIE['views'.$post_id.COOKIEHASH] == '1')

{

?

}

else{

setPostViews($post_id);

setcookie('views'.$post_id.COOKIEHASH,'1',time() + 3600,COOKIEPATH,COOKIE_DOMAIN);//设置时间间隔

}

?>

其实第一段代码和网上能找到的普通 wordpress 文章浏览量添加代码是一样的,重点是第二段的需要在文章页面 single 里面添加的这段代码起着决定性的作用。

方法二

此方法扒自某款插件,支持重复刷新不增加 wordpress 文章浏览量统计的代码,相对于博客吧前面分享的刷新不累加的 wordpress 文章浏览次数统计功能的教程代码,功能更加完整,代码更加完善,支持统计所有人的浏览和排除机器人的浏览量,有兴趣的博主可以参考或直接采用,懒人博主则可以直接使用 wp-postviews 插件。


1、在当前主题的 functions.php 文件中添加以下代码,作用是统计计数以及获取浏览数:

/***********文章统计*********/

function process_postviews() {

global $user_ID,$post;

if(check_cookie($post))

return;

if(is_int($post)) {

$post = get_post($post);

}

if(!wp_is_post_revision($post)) {

if(is_single() || is_page()) {

$id = intval($post->ID);

//$post_views = get_post_custom($id);

$post_views = get_post_meta($id,'_check_count',true);

//统计所有人

$should_count = true;

//排除机器人

$bots = array('Google Bot' => 'googlebot','Google Bot' => 'google','MSN' => 'msnbot','Alex' => 'ia_archiver','Lycos' => 'lycos','Ask Jeeves' => 'jeeves','Altavista' => 'scooter','AllTheWeb' => 'fast-webcrawler','Inktomi' => 'slurp@inktomi','Turnitin.com' => 'turnitinbot','Technorati' => 'technorati','Yahoo' => 'yahoo','Findexa' => 'findexa','NextLinks' => 'findlinks','Gais' => 'gaisbo','WiseNut' => 'zyborg','WhoisSource' => 'surveybot','Bloglines' => 'bloglines','BlogSearch' => 'blogsearch','PubSub' => 'pubsub','Syndic8' => 'syndic8','RadioUserland' => 'userland','Gigabot' => 'gigabot','Become.com' => 'become.com','Baidu Bot'=>'Baiduspider');

$useragent = $_SERVER['HTTP_USER_AGENT'];

foreach ($bots as $name => $lookfor) {

if (stristr($useragent,$lookfor) !== false) {

$should_count = false;

break;

}

}

if($should_count) {

if(!update_post_meta($id,($post_views+1))) {

add_post_meta($id,1,true);

}

}

}

}

}

?

function check_cookie($post){

$COOKNAME = 'ashuwp_view';

if(isset($_COOKIE[$COOKNAME]))

$cookie = $_COOKIE[$COOKNAME];

else

return false;

$id = $post->ID;

if(empty($id)){

return false;

}

if(!empty($cookie)){

$list = explode('a',$cookie);

if(!empty($list) && in_array($id,$list)){

return true;

}

}

return false;

}

### Function: Display The Post Views

function the_views($display = true,$id) {

$post_views = intval(get_post_meta($id,true));

$output = number_format_i18n($post_views);

if($display) {

echo $output;

} else {

return $output;

}

}

?

### Function: Display Total Views

if(!function_exists('get_totalviews')) {

function get_totalviews($display = true) {

global $wpdb;

$total_views = intval($wpdb->get_var("SELECT SUM(meta_value+0) FROM $wpdb->postmeta WHERE meta_key = '_check_count'"));

if($display) {

echo number_format_i18n($total_views);

} else {

return $total_views;

}

}

}

?

### Function: Add Views Custom Fields

add_action('publish_post','add_views_fields');

add_action('publish_page','add_views_fields');

function add_views_fields($post_ID) {

global $wpdb;

if(!wp_is_post_revision($post_ID)) {

add_post_meta($post_ID,true);

}

}

### Function: Delete Views Custom Fields

add_action('delete_post','delete_views_fields');

function delete_views_fields($post_ID) {

global $wpdb;

if(!wp_is_post_revision($post_ID)) {

delete_post_meta($post_ID,'_check_count');

}

}

2、一般只统计文章的浏览量,所以把下面的代码添加到当前主题 single.php 文件的第一行,代码作用是:用来设置 cookie,会在用户浏览器端增加一个形如: 123a45a45a113 其中字母 a 是分隔文章 ID 的,有效期是一天,由于设置 cookie 前不能有任何输出,所以这些代码要添加在文件的最最开头。

$COOKNAME = 'ashuwp_view'; //cookie名称

$TIME = 3600 * 24;

$PATH = '/';

?

$id = $posts[0]->ID;

$expire = time() + $TIME; //cookie有效期

if(isset($_COOKIE[$COOKNAME]))

$cookie = $_COOKIE[$COOKNAME]; //获取cookie

else

$cookie = '';

?

if(empty($cookie)){

//如果没有cookie

setcookie($COOKNAME,$id,$expire,$PATH);

}else{

//用a分割成数组

$list = explode('a',$cookie);

//如果已经存在本文的id

if(!in_array($id,$list)){

setcookie($COOKNAME,$cookie.'a'.$id,$PATH);

}

}

3、再在 single.php 文件的主循环部分(while( have_posts() ) : the_post();)后面自己喜欢的位置添加函数调用代码:

process_postviews();

4、在要显示浏览数的地方添加调用代码:

<?php the_views(true,$post->ID);?>

两种方法基本雷同,但是感觉第一种更简单点,具体那种方法更受欢迎更实用还需要大家的测试评估,这就是增强型 wordpress 文章浏览量统计支持重复刷新不增加计数的全部内容,需要大家能够用到不枉我们收集整理!

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读