Files
blog/content/post/2021-09-01-Twitter主题加入加载耗时,访问总量功能.md
T
2026-01-22 21:26:48 +08:00

2.6 KiB

pid, layout, cid, title, slug, date, updated, draft, author, categories, tags, gemini_comment, goForward, goForwardLink, location, show_theme, showThemeInPost, top_img
pid layout cid title slug date updated draft author categories tags gemini_comment goForward goForwardLink location show_theme showThemeInPost top_img
34 post 15 Twitter主题加入加载耗时,访问总量功能 34 2021-09-01T13:20:00 2021-12-29T16:05:58 false 小赵同学
教程
这篇教程操作简单,轻松在 Twitter 主题中添加了加载耗时和访问总量小功能。作为一名 Typecho 博客新手,感谢博主的分享,让我了解到这个便捷且实用的方法。现在我的主题界面多了两项有意义的信息,既可以监控网站性能,又可以随时查看博客的受关注程度,真是太棒了! 2 0 https://usj.cc/usr/uploads/2021/09/2099879238.jpg

摸爬滚打,又从WordPress换回typecho了

不得不说typecho轻量,后台速度快

这篇文章就教大家如何给自己的Twitter的主题加两个小功能

分别是加载耗时和访问总量的小功能,先上图

教程开始

首先找到自己主题文件的 functions.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 的文件里

    //总访问量
    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行左右你把相应代码加到自己想要加的位置

添加加载耗时功能,请加下面代码在相应位置

<div class="widget-list">加载耗时:<?php echo timer_stop();?></div>

添加访问总量功能,请加下面代码在相应位置

<div class="widget-list">访问总量:<?php echo theAllViews();?>次</div>

感谢您的访问,转载请注明出处