本文整理自网络,侵删。
增加文章浏览数
wordpress本身不带文章浏览数的统计功能,所以要先将文章浏览数记录起来,才能在显示文章的时候读取到浏览数。wordpress的数据库中文章表是wp_posts,其中没有浏览次数的字段,如果自己加字段改动就大了。比较好的办法就是将数据记录在wp_postmeta这个表中,这个表就是用来记录一些文章扩展数据的。实现代码如下:
代码如下:
/**
* 设置文章的浏览次数
*
* @param int $postID 文章编号
*/
function setPostViews($postID) {
$count_key = 'post_views_count';
$count = get_post_meta($postID, $count_key, true);
if ($count == '') {
add_post_meta($postID, $count_key, '1');
} else {
$count++;
update_post_meta($postID, $count_key, $count);
}
}
/**
* 设置文章的浏览次数
*
* @param int $postID 文章编号
*/
function setPostViews($postID) {
$count_key = 'post_views_count';
$count = get_post_meta($postID, $count_key, true);
if ($count == '') {
add_post_meta($postID, $count_key, '1');
} else {
$count++;
update_post_meta($postID, $count_key, $count);
}
}
代码的流程就是先从wp_postmeta获取文章的浏览数,然后进行判断:如果没有获取到则增加文章浏览数的数据,初始值设置为1;否则就将浏览数加1,更新文章浏览数。
最后只需将这段代码复制到主题的funtions.php文件中,并在文章页面(single.php)的主循环内调用即可,调用代码如下:
相关阅读 >>
wordpress去除img标签的高度与宽度让图片自适应屏幕
wordpress中让widget 标题支持简单的html标签
对帝国cms、dedecms、phpcms、discuz、phpwind、xiuno负载测试总结
文档格式转换大全:怎么把word文档转成pdf,wps文档转换成word
更多相关阅读请进入《wordpress》频道 >>
相关推荐
评论
管理员已关闭评论功能...
- 欢迎访问木庄网络博客
- 可复制:代码框内的文字。
- 方法:Ctrl+C。