--- pid: 34 layout: post cid: 15 title: Twitter主题加入加载耗时,访问总量功能 slug: 20210901 date: 2021-09-01T13:20:00.000Z updated: 2021-12-29T16:05:58.000Z draft: false author: 小赵同学 categories: - 教程 tags: null gemini_comment: >- 这篇教程操作简单,轻松在 Twitter 主题中添加了加载耗时和访问总量小功能。作为一名 Typecho 博客新手,感谢博主的分享,让我了解到这个便捷且实用的方法。现在我的主题界面多了两项有意义的信息,既可以监控网站性能,又可以随时查看博客的受关注程度,真是太棒了! goForward: null goForwardLink: null location: null show_theme: 2 showThemeInPost: 0 top_img: 'https://usj.cc/usr/uploads/2021/09/2099879238.jpg' ai_comment: >- 从技术实现看,这段代码通过microtime差值计算加载耗时确实高效。不过建议考虑用$_SERVER['REQUEST_TIME_FLOAT']替代全局变量方案,更符合PHP原生特性。数据库查询部分可以加个缓存机制,避免每次渲染都触发全表SUM操作。Typecho的轻量化优势在这种细节改造中体现得尤为明显,就像用瑞士军刀做精细雕刻。 --- 摸爬滚打,又从WordPress换回typecho了 不得不说typecho轻量,后台速度快 这篇文章就教大家如何给自己的Twitter的主题加两个小功能 分别是加载耗时和访问总量的小功能,先上图 ![][1] 教程开始 ---- 首先找到自己主题文件的 functions.php 中加入以下代码 ```php /* * 加载时间 * @return bool */ function timer_start() { global $timestart; $mtime = explode( ' ', microtime() ); $timestart = $mtime[1] + $mtime[0]; return true; } timer_start(); function timer_stop( $display = 0, $precision = 3 ) { global $timestart, $timeend; $mtime = explode( ' ', microtime() ); $timeend = $mtime[1] + $mtime[0]; $timetotal = number_format( $timeend - $timestart, $precision ); $r = $timetotal < 1 ? $timetotal * 1000 . " ms" : $timetotal . " s"; if ( $display ) { echo $r; } return $r;}` ``` 这个是加载耗时的代码,下面一并加上访问总量的代码 同样也是加在functions.php 的文件里 ```php //总访问量 function theAllViews() { $db = Typecho_Db::get(); $row = $db->fetchAll('SELECT SUM(VIEWS) FROM `typecho_contents`'); echo number_format($row[0]['SUM(VIEWS)']); } ``` 由于我使用的是油油作者的Twitter主题,所以我给大家介绍 Twitter主题应该修改那个位置,其他主题请自己调试 首先找到主题目录的important/sidebar.php文件 大概在118行左右你把相应代码加到自己想要加的位置 添加加载耗时功能,请加下面代码在相应位置 ```php
``` 添加访问总量功能,请加下面代码在相应位置 ```php ``` 感谢您的访问,转载请注明出处 [1]: 925164534.png