修改“Classic 22”主题记录(未完成)
这份终极总结不仅包含了咱们最终敲定的全部代码,还详细记录了每一步为什么要这么做(踩过的坑、底层逻辑和架构思路),可以作为你的建站备忘录永久保存。
一、核心架构思路(为什么这么改?)
- 数据库避坑:为了添加每条博文阅读次数显示,起初尝试直接在 SQLite 中给
contents表加views字段,导致整站白屏报错。最终决定利用 Typecho 原生的“自定义字段(fields表)”来存数据,彻底绕开了修改表结构带来的崩溃风险。 - “原版垫底,补丁压轴”:修改主题不再直接修改庞大且复杂的原版
style.css,而是启用主题自带的“自定义”模式。让原版style.css负责稳定的底层框架,把所有的改动(颜色、字号、图标)写到theme.css中,利用 CSS 的“后来居上”规则完美覆盖,保证升级不冲突。 - 移动端优先:页脚的修改放弃使用
<ul>/<li>,改用<span>配合flex-wrap,解决了手机端长文字强制换行导致排版撕裂的问题。
二、最终版代码汇总
1. functions.php(核心逻辑)
修改原因:为了增加阅读次数显示,加入 views 自定义字段的自动调用;利用 table.fields 进行读写以避免 SQLite 兼容问题;加入 1 小时 Cookie 防刷,防止 F5 狂刷;加入全站总数求和以在页脚显示总访问量。
<?php
if (!defined('__TYPECHO_ROOT_DIR__')) exit;
function themeConfig($form) {
// 原有配置项
$logoUrl = new \Typecho\Widget\Helper\Form\Element\Text(
'logoUrl', null, null, _t('网站 Logo'), _t('在这里填写图片 URL,网站将显示 Logo')
);
$form->addInput($logoUrl->addRule('url', _t('请填写正确的 URL 地址')));
$colorSchema = new \Typecho\Widget\Helper\Form\Element\Select(
'colorSchema', array(null => _t('自动'), 'light' => _t('浅色'), 'dark' => _t('深色'), 'customize' => _t('自定义')),
null, _t('外观风格'), _t('如果选择了自定义,主题将使用 theme.css 的样式')
);
$form->addInput($colorSchema);
}
// 1. 自定义字段:让编辑器自动出现 views 输入框
function themeFields($layout) {
$views = new \Typecho\Widget\Helper\Form\Element\Text(
'views', null, '0', _t('阅读量'), _t('请在此填写数字,新文章建议填 0。代码会自动累加。')
);
$layout->addItem($views);
}
// 2. 文章阅读量:安全读写 fields 表 + Cookie 防刷
function getPostViews($archive) {
if (!$archive->is('single')) {
$views = $archive->fields->views;
return $views ? (int)$views : 0;
}
$db = Typecho_Db::get();
$cid = $archive->cid;
$cookieName = 'typecho_views_' . $cid;
$row = $db->fetchRow($db->select('str_value')->from('table.fields')
->where('cid = ?', $cid)->where('name = ?', 'views'));
$views = $row ? (int)$row['str_value'] : 0;
if (!isset($_COOKIE[$cookieName])) {
$views++;
if ($row) {
$db->query($db->update('table.fields')
->rows(array('str_value' => (string)$views, 'int_value' => $views))
->where('cid = ?', $cid)->where('name = ?', 'views'));
} else {
$db->query($db->insert('table.fields')
->rows(array('cid' => $cid, 'name' => 'views', 'type' => 'str', 'str_value' => (string)$views, 'int_value' => $views)));
}
setcookie($cookieName, '1', time() + 3600, '/', '', false, true);
}
return $views;
}
// 3. 全站总访问量统计
function getAllViews() {
$db = Typecho_Db::get();
$row = $db->fetchRow($db->select(array('SUM(int_value)' => 'total_views'))->from('table.fields')->where('name = ?', 'views'));
$total = $row ? (int)$row['total_views'] : 0;
return number_format($total);
}
// 4. 页面 Meta 输出(含眼睛图标)——即文章下面的发表日期、分类、评论数,这里加上阅读次数
function postMeta(\Widget\Archive $archive, string $metaType = 'archive') {
?>
<header class="entry-header text-center">
<h1 class="entry-title" itemprop="name headline">
<a href="<?php $archive->permalink() ?>" itemprop="url"><?php $archive->title() ?></a>
</h1>
<?php if ($metaType != 'page'): ?>
<ul class="entry-meta list-inline text-muted">
<li class="feather-calendar"><time datetime="<?php $archive->date('c'); ?>" itemprop="datePublished"><?php $archive->date(); ?></time></li>
<li class="feather-folder"><?php $archive->category(', '); ?></li>
<li class="feather-eye"><?php echo getPostViews($archive); ?> 次阅读</li>
<li class="feather-message"><a href="<?php $archive->permalink() ?>#comments" itemprop="discussionUrl"><?php $archive->commentsNum(_t('暂无评论'), _t('1 条评论'), _t('%d 条评论')); ?></a></li>
</ul>
<?php endif; ?>
</header>
<?php
}2. footer.php(页脚结构)
修改原因:去掉不必要的 Typecho 链接精简内容;将访问统计提到第二项;通过 span + flex-wrap 实现手机端整项自动折行;纯手工嵌入 SVG 警徽,省去图片请求。
<?php if (!defined('__TYPECHO_ROOT_DIR__')) exit; ?>
<footer class="site-footer container-fluid">
<div class="text-muted" style="font-size: 0.875rem; display: flex; flex-wrap: wrap; justify-content: center; align-items: center; gap: 8px 16px; text-align: center;">
<span style="white-space: nowrap;">© <?php echo date('Y'); ?> <a href="<?php $this->options->siteUrl(); ?>"><?php $this->options->title(); ?></a></span>
<span style="white-space: nowrap;">访问量:<?php echo getAllViews(); ?> 次</span>
<span style="white-space: nowrap;"><a href="https://beian.miit.gov.cn/" target="_blank" rel="nofollow">ICP备XXXXXXXX号</a></span>
<span style="white-space: nowrap;">
<a href="https://beian.mps.gov.cn/" target="_blank" rel="nofollow" style="display: inline-flex; align-items: center; gap: 4px;">
<svg xmlns="http://www.w3.org/2000/svg" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" style="flex-shrink: 0;">
<path d="M12 22s8-4 8-10V5l-8-3-8 3v7c0 6 8 10 8 10z"></path>
</svg>
X公网安备XXXXXXXX号
</a>
</span>
</div>
</footer>
<?php $this->footer(); ?>
</body>
</html>3. theme.css(样式补丁)
修改原因:锁定 17px 基准字号防大屏过大;手工压小了两级标题;修复了导航栏和底部的宽度限制并保留了移动端边距;为文章元数据的阅读次数指定图标。
/* 1. 全局基准字号锁定为 17px */
:root {
--pico-font-size: 106.25% !important;
}
/* 2. 压小文章标题,大小等同 H3 */
.entry-title {
font-size: 1.5rem !important;
}
/* 3. 压小评论区标题,大小等同 H3 */
#comments h2.text-center {
font-size: 1.5rem !important;
}
/* 4. 限制导航栏和底部宽度 (修复手机端边距问题) */
@media (min-width: 840px) {
.site-navbar, .site-footer {
max-width: 840px !important;
margin-left: auto !important;
margin-right: auto !important;
}
}
/* 5. 压缩文章列表高度 */
.post-separator {
margin: 1.5rem 0 !important;
}
/* 6. 阅读量眼睛图标(路径已适配当前目录结构) */
.feather-eye:before {
content: "";
display: inline-block;
margin-right: 0.25rem;
background: url("./static/img/eye.svg") no-repeat center center/contain;
width: 1rem;
height: 1rem;
vertical-align: text-top;
} 标签:无