

WordPress免插件实现文章阅读数
source link: https://www.williamlong.info/archives/6920.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

WordPress免插件实现文章阅读数
一般为WordPress文章添加阅读次数统计,会用到各种统计插件,比较消耗系统资源,而且很多插件对于静态化的WordPress有冲突,这里就提供一种这里分享两段不用插件实现WordPress文章阅读次数的代码,支持全静态化的WordPress,供大家参考。
编辑文章页的模板文件,在需要显示阅读数的地方添加如下HTML代码:
<p>阅读:<?php echo "<span id=\"span_views\"></span><script type=\"text/javascript\">strBatchView=";the_ID();echo "</script>"; ?></p>
之后,在文章页模板底部添加如下HTML代码,
<script type='text/javascript'>
var js = document.createElement("script");
js.src = "/wp-counter.php?post_id=" + escape(strBatchView);
document.getElementsByTagName("body")[0].appendChild(js);
</script>
最后,下载“WP-PostViews的静态版”的JavaScript文件,将该文件放在网站根目录下即可。
这样,就可以实现WordPress免插件文章阅读数功能,并且支持全静态化的WordPress系统。

Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK