wordpress文章浏览次数统计轻量级代码版
首先在functions.php添加两个函数:
//获取浏览数-参数文章ID
function getPostViews($postID)
{
//字段名称
$count_key = 'post_views_count';
//获取字段值即浏览次数
$count = get_post_meta($postID, $count_key, true);
//如果为空设置为0
if($count=='')
{
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, '0');
return "浏览: 0 次";
}
return '浏览: '.$count.' 次';
}
//设置浏览数-参数文章ID
function setPostViews($postID)
{
//字段名称
$count_key = 'post_views_count';
//先获取获取字段值即浏览次数
$count = get_post_meta($postID, $count_key, true);
//如果为空就设为0
if($count=='')
{
$count = 0;
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, '0');
}
else
{
//如果不为空,加1,更新数据
$count++;
update_post_meta($postID, $count_key, $count);
}
}
