绿站
绿色文摘
今天是:

交流 | 搜索 | +发贴

WordPress免插件仅代码实现文章浏览次数
nydalu 2024年5月20日 21次

一、将下面的代码丢入主题下的funtions.php的文件的最后一个 ?> 内:
//文章浏览次数 Devework.com
function getPostViews($postID){
$count_key = ‘views’;
$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 = ‘views’;
$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{
//如果不为空,加1,更新数据
$count++;
update_post_meta($postID, $count_key, $count);
}
}
二、打开主题的single.php,在需要显示的地方添加以下代码调用:
浏览:setPostViews(get_the_ID());
echo number_format(getPostViews(get_the_ID())); 次



关于 ·登录·管理
加载0.324 查询 15