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

发布于 2012-04-09 | 更新于 2020-09-20

最近改了下主题,试着运行之后,发现使用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官网帮助文档中查找。

本文作者: arthinking

本文链接: https://www.itzhai.comuse-wp-postviews-plug-statistics-not-page-views.html

版权声明: 版权归作者所有,未经许可不得转载,侵权必究!联系作者请加公众号。

×
IT宅

关注公众号及时获取网站内容更新。