wordpress无插件实现文章浏览数
获取文章浏览量(pv),网上介绍了wp-views插件,安装过,没有效果。偶得一个方法。
第一步:将以下代码贴到主题的 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 View";
}
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);
}
}
第二步:在文章的内容循环代码里加上以下代码
<?php setPostViews(get_the_ID()); ?>
第三步:将以下代码放到任何需要显示文章浏览量的地方
<?php echo getPostViews(get_the_ID()); ?>
更多阅读
- 百度竞价开户状态查询工具1.0发布,营销拓客利器
- java 使用post获取ip归属地及运营商(附源代码)
- electron 报错 Error: Electron failed to install correctly, please delete node_modulees/electron and try installing again
- Java8 实现下载网页的完整代码
- windows 10 安装软件时出现740 createProcess错误解决办法
- 做SEO需要什么技能?十大技能提升SEO技术
- 宇秀下拉 2023-1-17号下拉更新案例
- java调用默认浏览器打开指定网页的方法(附源代码)
- windows 操作系统实现快速宽带拨号命令[批处理下载]
- 百度冰桶算法4.5更新:发力打击Landing Page恶劣广告行为

qq:1535604235


QQ
微信
商店
裕康网志
2017年10月8日 下午7:53
不见博主在用。
fedkey
2017年10月10日 下午10:26
好东西也未必全用啊。个人爱好简单点