非大众版WordPress纯代码实现阅读量统计
非大众版WordPress纯代码实现阅读量统计因疫情影响,已经2个多月没有更新博客了,如果现在还不赶快出来冒个泡,怕大家误认为我走丢了(因为从来没有这么长时间断更过)。 目前我使用的Begin知更鸟主题,文章阅读次数统计是依赖于PostViews插件,大家都知道WordPress
因疫情影响,已经2个多月没有更新博客了,如果现在还不赶快出来冒个泡,怕大家误认为我走丢了(因为从来没有这么长时间断更过)。
目前我使用的Begin知更鸟主题,文章阅读次数统计是依赖于PostViews插件,大家都知道WordPress的诟病就是插件越多越慢(其实在我看来并不是插件越多越慢,而是查询、事件钩子越多就越慢),所以自己就写了一个纯代码版本,实现了阅读统计和部分查询函数。
当然,可能你想说这些早就有人分享过了;是的,在我写之前我肯定也会上网看看已存的,发现有几点不如意,例如添加meta的钩子是放在wp_head
的、查询阅读数量必须要传递post_id
等。
我直接贴出代码,每个函数的功能均在注释中已经标识出:
<span class="hljs-comment">/** * 判断阅读数量是否需要增加并进行操作 * 转载请注明来自:https://www.vvso.cn/xlbk/17713.html */</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">the_views_add</span><span class="hljs-params">($post_ID,$count,$key)</span></span>{ <span class="hljs-keyword">if</span> (is_single() || is_page()) { <span class="hljs-keyword">if</span> ($count == <span class="hljs-string">''</span>) { add_post_meta($post_ID, $key, <span class="hljs-string">'0'</span>); } <span class="hljs-keyword">else</span> { update_post_meta($post_ID, $key, $count + <span class="hljs-number">1</span>); $count++; } } <span class="hljs-keyword">return</span> $count; } <span class="hljs-comment">//获取当前的阅读数量与自增</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">the_views</span> <span class="hljs-params">($post_id=null,$echo=true)</span> </span>{ <span class="hljs-keyword">global</span> $post; <span class="hljs-keyword">if</span>($post_id==<span class="hljs-keyword">null</span>){ $post_id = $post->ID; } $key = <span class="hljs-string">'views'</span>; $count = get_post_meta($post_id, $key, <span class="hljs-keyword">true</span>); <span class="hljs-keyword">if</span> ($count == <span class="hljs-string">''</span>) { $count = <span class="hljs-number">0</span>; } $count = the_views_add($post_id, $count, $key); $count = number_format_i18n($count); <span class="hljs-keyword">if</span>(!$echo){ <span class="hljs-keyword">return</span> $count; } <span class="hljs-keyword">echo</span> $count; } <span class="hljs-comment">//设置文章发布的时候进行字段添加</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">set_views</span> <span class="hljs-params">($post_ID)</span> </span>{ $key = <span class="hljs-string">'views'</span>; $count = get_post_meta($post_ID, $key, <span class="hljs-keyword">true</span>); <span class="hljs-keyword">if</span> ($count == <span class="hljs-string">''</span>) { add_post_meta($post_ID, $key, <span class="hljs-string">'0'</span>); } } add_action(<span class="hljs-string">'publish_post'</span>, <span class="hljs-string">'set_views'</span>);
这里再分享一个查询函数,因为是自定义的所以就没有插件的附带函数支持,这个函数是查询N天内阅读数量最多的文章:
<span class="hljs-comment">/** * 转载请注明来自:https://www.vvso.cn/xlbk/17713.html * 获取查看最多的文章 * <span class="hljs-doctag">@param</span> $days N天内 * <span class="hljs-doctag">@param</span> $nums 数量 * <span class="hljs-doctag">@return</span> array|object|null */</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">get_views_most_post</span><span class="hljs-params">($days, $nums)</span></span>{ <span class="hljs-keyword">global</span> $wpdb; $sql = <span class="hljs-string">"select posts.*, meta.meta_value as views from {$wpdb->posts} as posts INNER JOIN (select post_id,(meta_value+0) as meta_value from {$wpdb->postmeta} where meta_key='views' order by (meta_value+0) DESC) as meta on meta.post_id = posts.ID where posts.post_type = 'post' AND posts.post_status = 'publish' AND TO_DAYS(now()) - TO_DAYS(posts.post_date) < {$days} ORDER BY meta.meta_value DESC limit 0, {$nums}"</span>; <span class="hljs-keyword">return</span> $wpdb->get_results($sql); }
至于我为什么会分享这些代码出来,因为在过年期间博主写了一套WordPress主题,目前整体框架和内容都均已完成,现在处于优化和测试期,后续上线会专门发文告知,有兴趣的小伙伴欢迎多多关注,感谢你的支持!
版权所有:深圳市网商在线科技有限公司