wordpress主题制作当前文章阅读次数(纯代码实现无需插件)
# 前言
在一篇文章中需要显示当前文章阅读的次数时、可能需要使用插件来实现。但是wordpress本身就比较慢、在加上插件可能会导致网站更慢、那么就用纯代码形式去实现wordpress文章显示阅读次数!
# 修改functions.php文件
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, $count_key, '0');
return "0 次";
}
return $count.' 次';
}
function setPostViews($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{
$count++;
update_post_meta($postID, $count_key, $count);
}
}
# 调用阅读次数代码
<span>查阅次数:<?php echo getPostViews(get_the_ID()); ?> </span>
# 结尾
这样就可以实现纯代码方式去读取当前文章被查阅的次数了!是不是很简单、快来实际操作一番吧!
原创文章,作者:纸飞机,如若转载,请注明出处:https://www.zfjsec.com/525.html
-- 展开阅读全文 --