WordPress主题开发:设置和获取浏览次数
2017-10-27 15:58
676 查看
将以下代码放在functions.php,一个是获取阅读量,一个是设置阅读量
注意:调用了setPostViews函数后,每刷新一次就会增加一次浏览量。
在内容页(single.php,或page.php )尝试一下吧:
<?php /** * getPostViews()函数 * 功能:获取阅读数量 * 在需要显示浏览次数的位置,调用此函数 * @Param object|int $postID 文章的id * @Return string $count 文章阅读数量 */ 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; } /** * setPostViews()函数 * 功能:设置或更新阅读数量 * 在内容页(single.php,或page.php )调用此函数 * @Param object|int $postID 文章的id * @Return string $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 ); } } ?>
注意:调用了setPostViews函数后,每刷新一次就会增加一次浏览量。
在内容页(single.php,或page.php )尝试一下吧:
<?php setPostViews(get_the_ID());echo getPostViews( get_the_ID() ); ?>
相关文章推荐
- WordPress主题开发:设置和获取浏览次数
- inove主题通过wp-postviews插件的方法设置在主页及文章页设置浏览次数
- WordPress主题开发:通过page的ID或者别名获取内容
- wordpress 获取和设置文章的浏览数
- 在WordPress中获取数据库字段内容和添加主题设置菜单
- WordPress主题开发实例:get_term_by()获取指定分类链接
- 在WordPress中获取数据库字段内容和添加主题设置菜单
- WordPress主题开发实例:获取当前分类的文章列表
- WordPress 主题开发 - (六) 创建主题函数 待翻译
- wordpress 使用wp-postviews统计文章浏览次数和显示热门文章排行的方法
- 不用插件 WordPress 显示文章浏览次数
- WordPress官方主题开发文档
- WordPress主题开发:get_term_by和get_term_link
- WordPress主题开发:加载脚本和样式
- 【转】分享前端开发中通过js设置/获取cookie的一组方法
- WordPress主题开发:输出指定页面导航
- wordpress主题开发教程手记:自定义系统默认发信人
- WordPress 主题开发 - (一) 前言 待翻译
- 安卓开发中如何获取网络图片并设置到ImageView?
- WordPress主题开发实例:产品展示