wordpress无插件实现单篇文章浏览量统计

2013年10月11日

统计单篇文章的浏览量有助于博主分析访客的爱好和博客的流量分布,实现这方面功能的插件也很多。作为一个立志研究wordpress和php的博主,我更倾向于使用代码实现这个功能。经过查阅文档和百度相关资料,终于实现了这个功能,效果如本页面右上角“浏览次数”,由于博主初涉php和wordpress,功能还比较简单。
代码:

/****************************************************
 
功能:更新当前文章的浏览量。必须处于主循环当中。
参数:$postID: 当前文章的ID
返回:无
 
****************************************************/
 
function phsy_set_post_views($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{
        //administrator的浏览量不统计
        if(!current_user_can("administrator")){
            $count++;
            update_post_meta($postID, $count_key, $count);
        }
    }
}

 
这个是函数的功能是更新当前文章的浏览量,管理员的浏览不会统计在内,如果还需要过滤其他的角色浏览量,比如编辑者,把

if(!current_user_can(“administrator”))

 
改成

if(!current_user_can(“administrator”) && !current_user_can(“editor”))

 
即可
函数current_user_can($capability)的功能是判断当前用户的角色或者权限等级是否为$capability,如果是,返回true,否则返回false。
$capability可选参数:
administrator:管理员
editor:编辑者
author:作者
contributor:投稿者
subscriber:订阅者
下面这个函数的功能是获取并返回当前文章的浏览量。

/****************************************************
 
功能:获取当前文章的浏览量。必须处于主循环当中。
参数:$postID: 当前文章的ID
返回:浏览数量
 
****************************************************/
function phsy_get_post_views($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;
}

 
把这两个函数复制到当前主题目录下的functions.php中。
调用方法:
用编辑器打开当前主题目录下的single.php,查找

< ?php while (have_posts()) : the_post(); ?>

 
在这段代码后面加上

< ?php phsy_set_post_views(get_the_ID()); ?>

 
这样一来,浏览者在文章标题加载前就会被统计到一次浏览。
在想显示浏览数量的地方加上以下代码

< ?php echo phsy_get_post_views(get_the_ID()); ?>

 
注意:两个调用代码必须位于主循环当中,即

< ?php while (have_posts()) : the_post(); ?>

 

< ?php endwhile; ?>

 
之间
根据读者的建议,修改了一下函数,使其具有过滤刷新计数的功能,也就是说用户在关掉浏览器之前,每个用户每个页面只统计一次浏览量。基于session实现。
1.找到当前主题中的header.php文件,在标签之前,也就是整个文件的最前端加上下面这段代码:

< ?php session_start(); ?>

 
2.把phsy_set_post_views()那段代码换成下面这段

/****************************************************
 
功能:更新当前浏览文章的浏览量。必须处于主循环当中。
参数:$post_id:    当前文章的ID
返回:无
 
****************************************************/
 
function phsy_set_post_views($postID) {
    $the_ID = "arc".$postID;
    if(isset($_SESSION[$the_ID]) || $_SESSION[$the_ID] == 1)
    return;
    $_SESSION[$the_ID] = 1;
    $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, '1');
    }else{
        //administrator的浏览量不统计
        if(!current_user_can("administrator")){
            $count++;
            update_post_meta($postID, $count_key, $count);
        }
    }
}

 
使用方式和其他代码不变。

wordpress 插件 实现 单篇 文章 浏览量 统计

wordpress无插件实现单篇文章浏览量统计


wordpress 插件 实现 单篇 文章 浏览量 统计

wordpress无插件实现单篇文章浏览量统计


wordpress 插件 实现 单篇 文章 浏览量 统计

wordpress无插件实现单篇文章浏览量统计


wordpress 插件 实现 单篇 文章 浏览量 统计

wordpress无插件实现单篇文章浏览量统计


sicnature ---------------------------------------------------------------------
Your current IP address is: 54.226.94.217
Your IP address location: 美国弗吉尼亚阿什本
Your IP address country and region: 美国 美国
Your current browser is:
Your current system is:
Original content, please indicate the source:
同福客栈论坛 | 蟒蛇科普海南乡情论坛 | JiaYu Blog
sicnature ---------------------------------------------------------------------
Welcome to reprint. Please indicate the source http://www.myzhenai.com/post/1578.html

没有评论

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注