Posts tagged ‘浏览’

wordpress文章浏览次数统计轻量级代码版

首先在functions.php添加两个函数:

1//获取浏览数-参数文章ID
2function getPostViews($postID)
3{
4    //字段名称
5    $count_key = 'post_views_count';
6    //获取字段值即浏览次数
7    $count = get_post_meta($postID, $count_key, true);
8    //如果为空设置为0
9    if($count=='')
10    {
11        delete_post_meta($postID, $count_key);
12        add_post_meta($postID, $count_key, '0');
13        return "浏览: 0 次";
14    }
15    return '浏览: '.$count.' 次';
16}
17//设置浏览数-参数文章ID
18function setPostViews($postID)
19{
20    //字段名称
21    $count_key = 'post_views_count';
22    //先获取获取字段值即浏览次数
23    $count = get_post_meta($postID, $count_key, true);
24    //如果为空就设为0
25    if($count=='')
26    {
27        $count = 0;
28        delete_post_meta($postID, $count_key);
29        add_post_meta($postID, $count_key, '0');
30    }
31    else
32    {
33        //如果不为空,加1,更新数据
34        $count++;
35        update_post_meta($postID, $count_key, $count);
36    }
37}

Continue reading ‘wordpress文章浏览次数统计轻量级代码版’ »