Typecho常用代码收集

一、伪静态规则整理(Apache + NGINX)
1、Apache 环境(.htaccess)

RewriteEngine On
# 下面是在根目录,文件夹要修改路径,如 /oldtang/
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
2、NGINX 环境
location / {
index index.html index.php;
if (-f $request_filename/index.html) {
rewrite (.*) $1/index.html break;
}
if (-f $request_filename/index.php) {
rewrite (.*) $1/index.php;
}
if (!-f $request_filename) {
rewrite (.*) /index.php;
}
}
3、nginx二级目录
location /blog/ {
if (-f $request_filename/index.html){
rewrite (.*) $1/index.html break;
}
if (-f $request_filename/index.php){
rewrite (.*) $1/index.php last;
}
if (!-f $request_filename){
rewrite (.*) /blog/index.php last;
}
}
注意blog就是您自己的二级目录,请根据实际情况调整。

LAMP 建站教程,可以参考:《使用 LAMP 一键包部署 Linux 建站环境》。

如果需要配置 SSL,并做强制 https 跳转,可以参考《LAMP 环境下安装配置 SSL 证书并自动跳转到 HTTPS》。

二、开启gzip压缩,加速网站访问
Gzip是一种常用的HTML压缩技术,通常情况下,Gzip开启后会将输出到用户浏览器的数据进行压缩的处理,这样就会减小通过网络传输的数据量,提高浏览的速度。

Typecho的gzip压缩
开启Typecho的gzip功能很简单,是和wordpress一样的,只要在根目录下的index.php文件里面加上下面这一句就可以了:


ob_start('ob_gzhandler');
注意,是整个网站根目下的index.php文件,不是在主题目录下的。上面这句话建议加在最上面

三、升级全站https数据库批量替换网址/内容
内容
UPDATE `typecho_contents` SET `text` = REPLACE(`text`,'旧域名地址','新域名地址');
自定义
UPDATE `typecho_fields` SET `str_value` = REPLACE(`str_value`,'旧域名地址','新域名地址');
评论
UPDATE `typecho_comments` SET `text` = REPLACE(`text`,'https://','https://');
UPDATE `typecho_comments` SET `url` = REPLACE(`url`,'https://','https://');
四、TYPECHO支持emoji
最近写文章的时候用到了emoji,发现报错:Database Query Error

解决办法
1、进入数据库,直接运行下列语句:

alter table typecho_comments convert to character set utf8mb4 collate utf8mb4_general_ci;
alter table typecho_contents convert to character set utf8mb4 collate utf8mb4_general_ci;
alter table typecho_fields convert to character set utf8mb4 collate utf8mb4_general_ci;
alter table typecho_metas convert to character set utf8mb4 collate utf8mb4_general_ci;
alter table typecho_options convert to character set utf8mb4 collate utf8mb4_general_ci;
alter table typecho_relationships convert to character set utf8mb4 collate utf8mb4_general_ci;
alter table typecho_users convert to character set utf8mb4 collate utf8mb4_general_ci;
2、修改数据库的配置文件

网站根目录数据库配置文件config.inc.php,大约在60行


$db = new Typecho_Db('Pdo_Mysql', 'typecho_');
$db->addServer(array (
...
'charset' => 'utf8mb4', // 将原来的utf8修改为 utf8mb4
...
), Typecho_Db::READ | Typecho_Db::WRITE);
Typecho_Db::set($db);
五、模板添加备份恢复功能
image-20201005210752770

代码
在themeConfig($form)函数里添加

$db = Typecho_Db::get();
$sjdq=$db->fetchRow($db->select()->from ('table.options')->where ('name = ?', 'theme:Yodu'));
$ysj = $sjdq['value'];
if(isset($_POST['type']))
{
if($_POST["type"]=="备份模板数据"){
if($db->fetchRow($db->select()->from ('table.options')->where ('name = ?', 'theme:Yodubf'))){
$update = $db->update('table.options')->rows(array('value'=>$ysj))->where('name = ?', 'theme:Yodubf');
$updateRows= $db->query($update);
echo '

备份已更新,请等待自动刷新!如果等不到请点击';
?>
这里

}else{
if($ysj){
$insert = $db->insert('table.options')
->rows(array('name' => 'theme:Yodubf','user' => '0','value' => $ysj));
$insertId = $db->query($insert);
echo '

备份完成,请等待自动刷新!如果等不到请点击';
?>
这里

}
}
}
if($_POST["type"]=="还原模板数据"){
if($db->fetchRow($db->select()->from ('table.options')->where ('name = ?', 'theme:Yodubf'))){
$sjdub=$db->fetchRow($db->select()->from ('table.options')->where ('name = ?', 'theme:Yodubf'));
$bsj = $sjdub['value'];
$update = $db->update('table.options')->rows(array('value'=>$bsj))->where('name = ?', 'theme:Yodu');
$updateRows= $db->query($update);
echo '

检测到模板备份数据,恢复完成,请等待自动刷新!如果等不到请点击';
?>
这里

}else{
echo '

没有模板备份数据,恢复不了哦!

';
}
}
if($_POST["type"]=="删除备份数据"){
if($db->fetchRow($db->select()->from ('table.options')->where ('name = ?', 'theme:Yodubf'))){
$delete = $db->delete('table.options')->where ('name = ?', 'theme:Yodubf');
$deletedRows = $db->query($delete);
echo '

删除成功,请等待自动刷新,如果等不到请点击';
?>
这里

}else{
echo '

不用删了!备份不存在!!!

';
}
}
}
echo '

    

';
然后将里面出现的所有“yodu”改成你的模板目录的名字,如果拿不准就去数据库里看看模板的值名字。

备份
当用户点击备份时,先判断是否已经存在备份,如果不存在就插入一条新的数据,数据name为yodubf,value为模板原本的数据。此时就存在了一条备份数据。
如果再次点击备份按钮会发生什么呢?会触发更新数据的语句,就是读取模板的设置数据,然后将备份的模板数据更新。

还原
当用户点击还原按钮时,会判断是否存在备份,如果不存在就发出提示说不存在数据无法恢复;如果存在,就会进行一个反向的更新操作,将备份的数据更新到模板默认设置数据。

这个操作完成后会触发个小问题,比较影响体验的。就是在点击还原按钮时网页是先刷新后执行php还原语句的,也就是说还原完成后,你看到的模板设置页面数据并没有还原,但是实际数据库里面已经还原好了的,这一点很影响体验。

于是乎,我鸡贼的弄了个js自动刷新语句,并发出提示文字,这样一下子就友好多了,注意文章中代码方面我并未给出css样式,所以美观度上需要自行优化。

删除
删除就简单了,判断是否存在备份,不存在就告诉用户不用删了,你压根就没有备份数据,如果有备份就执行删除语句,发出提示。

一些没用的说明
1,其实这东西应该可以写成懒人版的,模板名字什么的用php获取下,就不用我这样写死了,但是当时我处于试一试的心态写的,所以就能简单就简单了,现在又懒得弄了,要不是为了水文,这个我都懒得贴出来。
2,别看文章中代码这么乱,条例就不清晰,其实我当时找了张纸写的逻辑然后才按照顺序一步一步的写的,也测试了很多回。
3,最开始想写自动还原模板数据来着,就是检测到模板启用就自动还原曾经的备份数据,然而当时想不通如果去判断模板启用。
4,当你想将本文章代码投入使用时,最好再测试博客进行测试,以免伤害你的数据库,同时建议测试时打开数据库管理页面,观看数据库对应表的变化

六、为Typecho添加webp解析
Typecho 原生不支持解析 Webp 图片,在附件插入 webp 文件会被当做文件解析,因此需要魔改 typecho。
废话不多说,上教程。

在 var/Widget/Abstract/Contents.php 中的 686 行左右:
将这行代码

$value['attachment']->isImage = in_array($content['type'], array('jpg', 'jpeg', 'gif', 'png', 'tiff', 'bmp'));
替换为

$value['attachment']->isImage = in_array($content['type'], array('jpg', 'jpeg', 'gif', 'png', 'tiff', 'bmp', 'webp'));
再到 var/Typecho/Common.php 的第 1193 行左右
添加如下代码

'webp' => 'image/webp',
最后到 Typecho 后台 -> 设置 -> 基本 -> 允许上传的文件类型 -> 添加 webp 即可完成解析。

七、其他
获取某个分类下的文章列表

$category = $this->widget('Widget_Archive@category', 'pageSize=6&type=category', 'mid=1');
while($category->next()){
// todo here
... ...
}
mid表示分类id,type指定获取分类文章
获取某关键词的搜索结果

$search = $this->widget('Widget_Archive@search', 'pageSize=10&type=search', 'keywords=typecho');
while($search->next()){
// todo here
... ...
}
type指定搜索类型,keywords指定搜索关键词
获取某个tag的文章列表

$tags = $this->widget('Widget_Archive@tag', 'pageSize=10&type=tag', 'mid=2');
//或者:$tags = $this->widget('Widget_Archive@tag', 'pageSize=10&type=tag', 'slug=tag_name');
while($tags->next()){
// todo here
... ...
}
type指定tag类型,mid表示tag的id,slug表示tag的缩写名
获取某篇特定的文章

$post = $this->widget('Widget_Archive@post', 'type=post', 'cid=1');
$post->title();
... ...
type指定post进而获取文章,cid指定文章id

站点名称
options->title() ?>

站点网址
options ->siteUrl(); ?>

站点说明
options->description() ?>

文章/页面的作者
author(); ?>

作者头像
< ?php $this->author->gravatar('40') ?>

上下篇调用代码
thePrev(); ?> theNext(); ?>

判断是否为首页,输出相关内容
is('index')): ?>
//是首页输出内容

//不是首页输出内容

文章/页面评论数目
commentsNum('No Comments', '1 Comment' , '%d Comments'); ?>

截取文章内容显示摘要(350是字符数)
excerpt(350, '.. .'); ?>

调用自定义字段
fields->fieldName ?>

RSS地址
options->feedUrl(); ?>

获取最新评论列表
widget('Widget_Comments_Recent')->to($comments); ?> next()): ?>
{date}; ?>">author(false); ?>: excerpt(50, '...'); ?> 分类名称(无链接)category(',', false); ?>获取文章时间归档 widget('Widget_Contents_Post_Date', 'type=month&format=F Y') ->parse('
'); ?>

获取标签集合
widget('Widget_Metas_Tag_Cloud', 'ignoreZeroCount=1&limit=28')->to($tags); ?>
next()): ?>
name(); ?>

登陆与未登录用户展示不同内容
user->hasLogin()): ?>
//登陆可见

//未登录和登陆均可见

自动调用img字段内容,如果没有,去文章搜索第1个图片。
___fields()))): ?>fields->img(); ?> preg_match_all("/\&lt;img.*?src\=(\'|\")(.*?)(\'|\")[^>]*>/i", $this->content, $matches);
$imgCount = count($matches[0]);
if($imgCount >= 1){
$img = $matches[2][0];
echo << {$img}
Html;
}
?>

文章内容替换七牛网址
content); ?>

文章字数统计
在functions.php中写入代码:
function art_count ($cid){
$db=Typecho_Db::get ();
$rs=$db->fetchRow ($db->select ('table.contents.text')->from ('table.contents')->where ('table.contents.cid=?',$cid)->order ('table.contents.cid',Typecho_Db::SORT_ASC)->limit (1));
echo mb_strlen($rs['text'], 'UTF-8');
}

在模板中调用:
cid); ?>

自动调用第1个文章图片
preg_match_all("/\&lt;img.*?src\=(\'|\")(.*?)(\'|\")[^>]*>/i", $this->content, $matches);
$imgCount = count($matches[0]);
if($imgCount >= 1){
$img = $matches[2][0];
echo <<


{$this->title}

Html;
}
?>

边栏不显示博主评论
widget('Widget_Comments_Recent','ignoreAuthor=true')->to($comments); ?>

前台登录表单




......

发表新评论