WordPress主题开发:设置和获取浏览次数
将以下代码放在functions.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() ); ?>
参考文档:
http://wp-snippets.com/post-views-without-plugin/
WordPress主题开发:设置和获取浏览次数的更多相关文章
- WordPress主题开发实例:获取当前分类的文章列表
思路: 如果使用默认的wordpress的方法,调出来的数据就会被后台的显示个数所限制,而我们需要的是自由控制文章数和翻页,所以我使用WP_Query 获取当前分类的方法可以通过 get_query_ ...
- WordPress主题开发: 制作文章页面single.php
可以调用的文章内容: 调用文章标题:<?php the_title(); ?> 调用文章内容:<?php the_content(); ?> 调用文章摘要:<?php t ...
- wordpress 主题开发
https://yusi123.com/3205.html https://themeshaper.com/2012/10/22/the-themeshaper-wordpress-theme-tut ...
- 黄聪:《跟黄聪学WordPress主题开发》
又一个作品完成!<跟黄聪学Wordpress主题开发>,国内最好的Wordpress主题模版开发视频教程!! 目录预览: WordPress官方源文件层式结构讲解 WordPress数据库 ...
- [转]WordPress主题开发:主题初始化
本文转自:http://www.cnblogs.com/tinyphp/p/4391182.html 在最简单的情况下,一个WordPress主题由两个文件构成: index.php -------- ...
- WordPress主题开发:主题初始化
在最简单的情况下,一个WordPress主题由两个文件构成: index.php ------------------主模版 style.css -------------------主样式表(注意 ...
- WordPress 主题开发 - (三) 开发工具 待翻译
Before we get started building any WordPress Theme, we’re going to need to get our development tools ...
- WordPress 主题开发:从入门到精通(必读)
本专栏介绍如何开发设计你自己的 WordPress 主题.如果你希望了解更多如何安装和应用主题的内容,请参阅应用主题文档.本文的内容不同于应用主题,因为所讨论的是编写代码去构建你自己的主题的技术内容, ...
- WordPress 主题开发 - (一) 前言 待翻译
原文出自: http://themeshaper.com/2012/10/22/the-themeshaper-wordpress-theme-tutorial-2nd-edition/ THE TH ...
随机推荐
- 重温CSS之文档结构
我们来看看几个基本的HTML页面 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "h ...
- 【linux】crontab无效解决办法
参考来源:http://www.tennfy.com/3088.html 问题: 我在crontab中添加了一条定时任务 * * * sh test.sh 出乎意料的是,该定时任务并没有执行.可是我直 ...
- 微信小程序开发之IOS/Android兼容坑(持续更新)
一.时间转换问题: 这不只是小程序上面的问题是ios系统 都有这个问题就是new Date("2017-06-16") 在IOS会出现NAN的情况所以对于时间转换需要另行封装,解 ...
- node 使用笔记
1 安装 buffertools 因为使用mjpeg-proxy的关系,需要编译buffertools中的C++代码文件,怎奈何一直出错. make: Entering directory `/srv ...
- [转] map/reduce
如果你读过Google的那篇大名鼎鼎的论文“MapReduce: Simplified Data Processing on Large Clusters”,你就能大概明白map/reduce的概念. ...
- 【LOJ】#6433. 「PKUSC2018」最大前缀和
题解 神仙的状压啊QAQ 设一个\(f[S]\)表示数字的集合为\(S\)时\(sum[S]\)为前缀最大值的方案数 \(g[S]\)表示数字集合为\(S\)时所有前缀和都小于等于0的方案数 答案就是 ...
- jQuery选择器(转)
原文:http://www.cnblogs.com/qinwang913/p/3444135.html 一.基本分类 jQuery选择器大致可以分为两类,基本选择器和过滤选择器,总体结构体系如下: ...
- PMP 考试 形式
200 道单选题- 考试时间为4个小时(上午9点到下午1点) 1年4次考试时间 2017年 3月18 6月24 9月9 10月9 考试费用3300 PMBOK
- 蓝牙设备探测工具blueranger
蓝牙设备探测工具blueranger blueranger是Kali Linux预安装的一款蓝牙探测工具.该工具通过向指定设备发送蓝牙L2CAP协议的ping包,创建连接.由于大部分蓝牙设备对pi ...
- 不同浏览器的userAgent
一.IE浏览器 //IE6 "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)" //IE7 "Mozill ...