0%
这是一片思考的空间 -- arthinking
Spring 重构&代码整洁之道 软件设计 JVM 并发编程 数据结构与算法 分布式 存储 网络 微服务 设计模式
Java技术栈 - 涉及Java技术体系

使用WP-PostViews插件统计不了浏览量的问题

最近改了下主题,试着运行之后,发现使用WP-PostViews插件不能统计浏览量,在single.php页面中使用了之后显示的浏览次数一直是0,即使别人浏览过了。

在后台的插件列表中找到WP-PostViews,并点击编辑,打开其源代码,找到了如下函数

### Function: Calculate Post Views
add_action('wp_head', 'process_postviews');
function process_postviews() {..}

可以发现这个process_postviews()函数就是统计浏览次数的函数,在这里使用了这样一句代码:

add_action('wp_head', 'process_postviews');

经过在wordpress的帮助文档中找到关于两个函数的说明:

add_action():

http://codex.wordpress.org/Function_Reference/add_action

Hooks a function on to a specific action.

wp_head():

http://codex.wordpress.org.cn/Plugin_API/Action_Reference/wp_head

ttwp_head()/tt is triggered within the tthead/head/tt section of the user's template by the ttwp_head()/tt function. Although this is theme-dependent, it is one of the most essential theme hooks, so it is fairly widely supported.

知道了,这句代码是在wp_head执行时添加自定义的一些动作,这里就是添加了这个统计访问数的函数process_postviews(),为了让这句话能够被执行,需要在wordpress主题程序中添加上wp_head()这个函数,以便能够执行到被添加的process_postviews()函数。

所以解决的方法就是在header.php文件中的标签中添加上这个函数:

这样就可以正常统计浏览量了,与之类似的函数还有wp_footer()。

remove_action()

既然有add_action,那么wordpress应该有提供remove_action吧。经过查阅,发现真有这个函数。Add_action是在需要的执行阶段添加自定义的执行代码,那么remove_action就是删除在执行阶段会被默认执行的代码。

比如在head标签中添加了wp_head() 函数,wordpress会生成很多其他的代码,其实有些也没有必要返回给客户端,这样,我们就可以使用remove_action删除掉不必要的执行步骤了,下面是从网上看到的一个优化wp_head()函数而添加的一个代码片段,把以下代码放入到functions.php中即可:

//wp_head()函数优化
remove_action( 'wp_head', 'feed_links_extra', 3 ); // Display the links to the extra feeds such as category feeds
remove_action( 'wp_head', 'feed_links', 2 ); // Display the links to the general feeds: Post and Comment Feed
remove_action( 'wp_head', 'rsd_link' ); // Display the link to the Really Simple Discovery service endpoint, EditURI link
remove_action( 'wp_head', 'wlwmanifest_link' ); // Display the link to the Windows Live Writer manifest file.
remove_action( 'wp_head', 'index_rel_link' ); // index link
remove_action( 'wp_head', 'parent_post_rel_link', 10, 0 ); // prev link
remove_action( 'wp_head', 'start_post_rel_link', 10, 0 ); // start link
remove_action( 'wp_head', 'adjacent_posts_rel_link', 10, 0 ); // Display relational links for the posts adjacent to the current post.
remove_action( 'wp_head', 'wp_generator' ); // Display the XHTML generator that is generated on the wp_head hook, WP version

不过,在去除这些函数时,还需要考虑自己是否需要用到这些函数哦。比如rsd_link()和wlwmanifest_link()这两个函数是Wordpress为博客客户端软件添加的,如果你的Wordpress使用到了类似 Live Writer这类软件在编写, 就需要这两个函数,其他的函数含义参考上面代码中的注释或者在wordpress官网帮助文档中查找。

欢迎关注我的其它发布渠道

订阅IT宅
内功修炼
Java技术栈
Java架构杂谈是IT宅精品文章公众号,欢迎订阅:
📄 网络基础知识:两万字长文50+张趣图带你领悟网络编程的内功心法 📄 HTTP发展史:三万长文50+趣图带你领悟web编程的内功心法 📄 HTTP/1.1:可扩展,可靠性,请求应答,无状态,明文传输 📄 HTTP/1.1报文详解:Method,URI,URL,消息头,消息体,状态行 📄 HTTP常用请求头大揭秘 📄 HTTPS:网络安全攻坚战 📄 HTTP/2:网络安全传输的快车道 📄 HTTP/3:让传输效率再一次起飞 📄 高性能网络编程:图解Socket核心内幕以及五大IO模型 📄 高性能网络编程:三分钟短文快速了解信号驱动式IO 📄 高性能网络编程:彻底弄懂IO复用 - IO处理杀手锏,带您深入了解select,poll,epoll 📄 高性能网络编程:异步IO:新时代的IO处理利器 📄 高性能网络编程:网络编程范式 - 高性能服务器就这么回事 📄 高性能网络编程:性能追击 - 万字长文30+图揭秘8大主流服务器程序线程模型
📄 Java内存模型:如果有人给你撕逼Java内存模型,就把这些问题甩给他 📄 一文带你彻底理解同步和锁的本质(干货) 📄 AQS与并发包中锁的通用实现 📄 ReentrantLock介绍与使用 📄 ReentrantReadWriteLock介绍与使用 📄 ReentrantLock的Condition原理解析 📄 如何优雅的中断线程 📄 如何优雅的挂起线程 📄 图解几个好玩的并发辅助工具类 📄 图解BlockingQueue阻塞队列
📄 消息队列那么多,为什么建议深入了解下RabbitMQ? 📄 高并发异步解耦利器:RocketMQ究竟强在哪里? 📄 Kafka必知必会18问:30+图带您看透Kafka
📄 洞悉MySQL底层架构:游走在缓冲与磁盘之间 📄 SQL运行内幕:从执行原理看调优的本质 📄 洞悉Redis技术内幕:缓存,数据结构,并发,集群与算法