SiteStatFooter

页脚展示统计信息,建站时间可以在插件管理中配置

使用方法

  1. 在 Typecho 根目录下的 usr/plugins 目录中,创建新目录 SiteStatFooter
  2. 将下面的这段代码保存为 Plugin.php
  3. 登录 Typecho 管理后台;
  4. 进入 控制台 → 插件 页面;
  5. 启用 SiteStatFooter 插件;
  6. 打开博客任意页面,在页面最底部应该能看到:
📝 文章:21 💬 评论:3 🏷️ 标签:21 📁 分类:6 ⏳ 运行:65 天

Plugin.php 内容为:

<?php

namespace TypechoPlugin\SiteStatFooter;

use Typecho\Plugin\PluginInterface;
use Typecho\Widget\Helper\Form;
use Typecho\Widget\Helper\Form\Element\Text;

if (!defined('__TYPECHO_ROOT_DIR__')) {
    exit;
}

/**
 * SiteStatFooter 插件:在前台页脚显示文章、评论、标签、分类数以及建站运行时间
 *
 * @package SiteStatFooter
 * @author GPT
 * @version 1.1.0
 * @link https://example.com
 */
class Plugin implements PluginInterface
{
    /**
     * 插件启用时注册前台页脚钩子
     */
    public static function activate()
    {
        \Typecho\Plugin::factory('Widget_Archive')->footer = __CLASS__ . '::footer';
    }

    /**
     * 插件禁用时
     */
    public static function deactivate() {}

    /**
     * 插件配置界面
     *
     * @param Form $form
     */
    public static function config(Form $form)
    {
        $siteStart = new Text(
            'siteStart',
            null,
            '2022-01-01',
            _t('建站日期'),
            _t('用于计算运行天数,格式为 YYYY-MM-DD')
        );
        $form->addInput($siteStart->addRule('required', _t('请填写建站日期')));
    }

    /**
     * 用户个性化配置(可选)
     */
    public static function personalConfig(Form $form) {}

    /**
     * 插件核心功能:输出统计信息到前台页脚
     */
    public static function footer()
    {
        $options = \Typecho_Widget::widget('Widget_Options')->plugin('SiteStatFooter');
        $siteStart = isset($options->siteStart) ? strtotime($options->siteStart) : strtotime('2022-01-01');
        $days = floor((time() - $siteStart) / 86400);

        $stat = \Typecho_Widget::widget('Widget_Stat');

        echo '<div class="footer-stat" style="padding: 1em 0; font-size: 0.85em; color: #666; line-height: 1.6; text-align: center;">';
        echo '<span style="margin-right: 1em;">📝 文章:<strong>' . $stat->publishedPostsNum . '</strong></span>';
        echo '<span style="margin-right: 1em;">💬 评论:<strong>' . $stat->publishedCommentsNum . '</strong></span>';
        echo '<span style="margin-right: 1em;">🏷️ 标签:<strong>' . $stat->tagsNum . '</strong></span>';
        echo '<span style="margin-right: 1em;">📁 分类:<strong>' . $stat->categoriesNum . '</strong></span>';
        echo '<span>⏳ 运行:<strong>' . $days . '</strong> 天</span>';
        echo '</div>';
    }
}

标签: typecho, 插件, 个性化

添加新评论