Typecho常用代码收集

  • 💡Typecho
  • 3684 阅读
  • 2020年09月14日
  • 0 条评论
  • 全文共1649字, 阅读大约需要5分钟
  • 搜索引擎已收录

首页 / 💡Typecho / 正文

AI摘要
Gemini 1.5 Pro
|
此内容根据文章生成,并经过人工审核,仅用于文章内容的解释与总结
反馈

一、伪静态规则整理(Apache + NGINX)

1、Apache 环境(.htaccess)

  1. <IfModule mod_rewrite.c>
  2. RewriteEngine On
  3. # 下面是在根目录,文件夹要修改路径,如 /oldtang/
  4. RewriteBase /
  5. RewriteCond %{REQUEST_FILENAME} !-f
  6. RewriteCond %{REQUEST_FILENAME} !-d
  7. RewriteRule ^(.*)$ /index.php/$1 [L]
复制代码

2、NGINX 环境

  1. location / {
  2. index index.html index.php;
  3. if (-f $request_filename/index.html) {
  4. rewrite (.*) $1/index.html break;
  5. }
  6. if (-f $request_filename/index.php) {
  7. rewrite (.*) $1/index.php;
  8. }
  9. if (!-f $request_filename) {
  10. rewrite (.*) /index.php;
  11. }
  12. }
复制代码

3、nginx二级目录

  1. location /blog/ {
  2. if (-f $request_filename/index.html){
  3. rewrite (.*) $1/index.html break;
  4. }
  5. if (-f $request_filename/index.php){
  6. rewrite (.*) $1/index.php last;
  7. }
  8. if (!-f $request_filename){
  9. rewrite (.*) /blog/index.php last;
  10. }
  11. }
复制代码

注意blog就是您自己的二级目录,请根据实际情况调整。

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

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

二、开启gzip压缩,加速网站访问

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

Typecho的gzip压缩

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

  1. /*添加Gzip*/
  2. ob_start('ob_gzhandler');
复制代码

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

三、升级全站https数据库批量替换网址/内容

内容

  1. UPDATE `typecho_contents` SET `text` = REPLACE(`text`,'旧域名地址','新域名地址');
复制代码

自定义

  1. UPDATE `typecho_fields` SET `str_value` = REPLACE(`str_value`,'旧域名地址','新域名地址');
复制代码

评论

  1. UPDATE `typecho_comments` SET `text` = REPLACE(`text`,'http://','https://');
  2. UPDATE `typecho_comments` SET `url` = REPLACE(`url`,'http://','https://');
复制代码

四、TYPECHO支持emoji

最近写文章的时候用到了emoji,发现报错:Database Query Error

解决办法

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

  1. alter table typecho_comments convert to character set utf8mb4 collate utf8mb4_general_ci;
  2. alter table typecho_contents convert to character set utf8mb4 collate utf8mb4_general_ci;
  3. alter table typecho_fields convert to character set utf8mb4 collate utf8mb4_general_ci;
  4. alter table typecho_metas convert to character set utf8mb4 collate utf8mb4_general_ci;
  5. alter table typecho_options convert to character set utf8mb4 collate utf8mb4_general_ci;
  6. alter table typecho_relationships convert to character set utf8mb4 collate utf8mb4_general_ci;
  7. alter table typecho_users convert to character set utf8mb4 collate utf8mb4_general_ci;
复制代码

2、修改数据库的配置文件

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

  1. /** 定义数据库参数 */
  2. $db = new Typecho_Db('Pdo_Mysql', 'typecho_');
  3. $db->addServer(array (
  4. ...
  5. 'charset' => 'utf8mb4', // 将原来的utf8修改为 utf8mb4
  6. ...
  7. ), Typecho_Db::READ | Typecho_Db::WRITE);
  8. Typecho_Db::set($db);
复制代码

五、模板添加备份恢复功能

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

  1. $db = Typecho_Db::get();
  2. $sjdq=$db->fetchRow($db->select()->from ('table.options')->where ('name = ?', 'theme:Yodu'));
  3. $ysj = $sjdq['value'];
  4. if(isset($_POST['type']))
  5. {
  6. if($_POST["type"]=="备份模板数据"){
  7. if($db->fetchRow($db->select()->from ('table.options')->where ('name = ?', 'theme:Yodubf'))){
  8. $update = $db->update('table.options')->rows(array('value'=>$ysj))->where('name = ?', 'theme:Yodubf');
  9. $updateRows= $db->query($update);
  10. echo '<div class="tongzhi">备份已更新,请等待自动刷新!如果等不到请点击';
  11. ?>
  12. <a href="<?php Helper::options()->adminUrl('options-theme.php'); ?>">这里</a></div>
  13. <script language="JavaScript">window.setTimeout("location=\'<?php Helper::options()->adminUrl('options-theme.php'); ?>\'", 2500);</script>
  14. <?php
  15. }else{
  16. if($ysj){
  17. $insert = $db->insert('table.options')
  18. ->rows(array('name' => 'theme:Yodubf','user' => '0','value' => $ysj));
  19. $insertId = $db->query($insert);
  20. echo '<div class="tongzhi">备份完成,请等待自动刷新!如果等不到请点击';
  21. ?>
  22. <a href="<?php Helper::options()->adminUrl('options-theme.php'); ?>">这里</a></div>
  23. <script language="JavaScript">window.setTimeout("location=\'<?php Helper::options()->adminUrl('options-theme.php'); ?>\'", 2500);</script>
  24. <?php
  25. }
  26. }
  27. }
  28. if($_POST["type"]=="还原模板数据"){
  29. if($db->fetchRow($db->select()->from ('table.options')->where ('name = ?', 'theme:Yodubf'))){
  30. $sjdub=$db->fetchRow($db->select()->from ('table.options')->where ('name = ?', 'theme:Yodubf'));
  31. $bsj = $sjdub['value'];
  32. $update = $db->update('table.options')->rows(array('value'=>$bsj))->where('name = ?', 'theme:Yodu');
  33. $updateRows= $db->query($update);
  34. echo '<div class="tongzhi">检测到模板备份数据,恢复完成,请等待自动刷新!如果等不到请点击';
  35. ?>
  36. <a href="<?php Helper::options()->adminUrl('options-theme.php'); ?>">这里</a></div>
  37. <script language="JavaScript">window.setTimeout("location=\'<?php Helper::options()->adminUrl('options-theme.php'); ?>\'", 2000);</script>
  38. <?php
  39. }else{
  40. echo '<div class="tongzhi">没有模板备份数据,恢复不了哦!</div>';
  41. }
  42. }
  43. if($_POST["type"]=="删除备份数据"){
  44. if($db->fetchRow($db->select()->from ('table.options')->where ('name = ?', 'theme:Yodubf'))){
  45. $delete = $db->delete('table.options')->where ('name = ?', 'theme:Yodubf');
  46. $deletedRows = $db->query($delete);
  47. echo '<div class="tongzhi">删除成功,请等待自动刷新,如果等不到请点击';
  48. ?>
  49. <a href="<?php Helper::options()->adminUrl('options-theme.php'); ?>">这里</a></div>
  50. <script language="JavaScript">window.setTimeout("location=\'<?php Helper::options()->adminUrl('options-theme.php'); ?>\'", 2500);</script>
  51. <?php
  52. }else{
  53. echo '<div class="tongzhi">不用删了!备份不存在!!!</div>';
  54. }
  55. }
  56. }
  57. echo '<form class="protected" action="?yodubf" method="post">
  58. <input type="submit" name="type" class="btn btn-s" value="备份模板数据" />&nbsp;&nbsp;<input type="submit" name="type" class="btn btn-s" value="还原模板数据" />&nbsp;&nbsp;<input type="submit" name="type" class="btn btn-s" value="删除备份数据" /></form>';
复制代码

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

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

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

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

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

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

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

六、为Typecho添加webp解析

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

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

  1. $value['attachment']->isImage = in_array($content['type'], array('jpg', 'jpeg', 'gif', 'png', 'tiff', 'bmp'));
复制代码

替换为

  1. $value['attachment']->isImage = in_array($content['type'], array('jpg', 'jpeg', 'gif', 'png', 'tiff', 'bmp', 'webp'));
复制代码

再到 var/Typecho/Common.php 的第 1193 行左右
添加如下代码

  1. 'webp' => 'image/webp',
复制代码

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


七、其他

获取某个分类下的文章列表

  1. $category = $this->widget('Widget_Archive@category', 'pageSize=6&type=category', 'mid=1');
  2. while($category->next()){
  3. // todo here
  4. ... ...
  5. }
  6. mid表示分类id,type指定获取分类文章
复制代码

获取某关键词的搜索结果

  1. $search = $this->widget('Widget_Archive@search', 'pageSize=10&type=search', 'keywords=typecho');
  2. while($search->next()){
  3. // todo here
  4. ... ...
  5. }
  6. type指定搜索类型,keywords指定搜索关键词
复制代码

获取某个tag的文章列表

  1. $tags = $this->widget('Widget_Archive@tag', 'pageSize=10&type=tag', 'mid=2');
  2. //或者:$tags = $this->widget('Widget_Archive@tag', 'pageSize=10&type=tag', 'slug=tag_name');
  3. while($tags->next()){
  4. // todo here
  5. ... ...
  6. }
  7. type指定tag类型,mid表示tag的id,slug表示tag的缩写名
复制代码

获取某篇特定的文章

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

站点名称

  1. <?php $this->options->title() ?>
复制代码

站点网址

  1. <?php $this->options ->siteUrl(); ?>
复制代码

站点说明

  1. <?php $this->options->description() ?>
复制代码

文章/页面的作者

  1. <?php $this->author(); ?>
复制代码

作者头像

  1. < ?php $this->author->gravatar('40') ?>
复制代码

上下篇调用代码

  1. <?php $this->thePrev(); ?> <?php $this->theNext(); ?>
复制代码

判断是否为首页,输出相关内容

  1. <?php if ($this->is('index')): ?>
  2. //是首页输出内容
  3. <?php else: ?>
  4. //不是首页输出内容
  5. <?php endif; ?>
  6. 文章/页面评论数目
  7. <?php $this->commentsNum('No Comments', '1 Comment' , '%d Comments'); ?>
复制代码

截取文章内容显示摘要(350是字符数)

  1. <?php $this->excerpt(350, '.. .'); ?>
复制代码

调用自定义字段

  1. <?php $this->fields->fieldName ?>
复制代码

RSS地址

  1. <?php $this->options->feedUrl(); ?>
复制代码

获取最新评论列表

  1. <?php $this->widget('Widget_Comments_Recent')->to($comments); ?> <?php while($comments->next()): ?>[{date}](https://www.dpaoz.com/permalink(); ?>">author(false); ?>: excerpt(50, '...'); ?> 分类名称(无链接)category(',', false); ?>获取文章时间归档 widget('Widget_Contents_Post_Date', 'type=month&format=F Y') ->parse(''); ?>
复制代码

获取标签集合

  1. <?php $this->widget('Widget_Metas_Tag_Cloud', 'ignoreZeroCount=1&limit=28')->to($tags); ?>
  2. <?php while($tags->next()): ?>
  3. <a href="<?php $tags->permalink(); ?>" class="size-<?php $tags->split(5, 10, 20, 30); ?>"><?php $tags->name(); ?></a>
  4. <?php endwhile; ?>
复制代码

登陆与未登录用户展示不同内容

  1. <?php if($this->user->hasLogin()): ?>
  2. //登陆可见
  3. <?php else: ?>
  4. //未登录和登陆均可见
  5. <?php endif; ?>
复制代码

自动调用img字段内容,如果没有,去文章搜索第1个图片。

  1. <?php if (array_key_exists('img',unserialize($this->___fields()))): ?><?php $this->fields->img(); ?><?php else: ?><?php
  2. preg_match_all("/\&amp;lt;img.*?src\=(\'|\")(.*?)(\'|\")[^>]*>/i", $this->content, $matches);
  3. $imgCount = count($matches[0]);
  4. if($imgCount >= 1){
  5. $img = $matches[2][0];
  6. echo <<<Html
  7. {$img}
  8. Html;
  9. }
  10. ?><?php endif; ?>
复制代码

文章内容替换七牛网址

  1. <?php echo $str = str_replace("your.com/usr/uploads","your.qiniu.com/usr/uploads",$this->content); ?>
复制代码

文章字数统计

在functions.php中写入代码:

  1. function art_count ($cid){
  2. $db=Typecho_Db::get ();
  3. $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));
  4. echo mb_strlen($rs['text'], 'UTF-8');
  5. }
复制代码

在模板中调用:

  1. <?php echo art_count($this->cid); ?>
复制代码

自动调用第1个文章图片

  1. <?php
  2. preg_match_all("/\&amp;lt;img.*?src\=(\'|\")(.*?)(\'|\")[^>]*>/i", $this->content, $matches);
  3. $imgCount = count($matches[0]);
  4. if($imgCount >= 1){
  5. $img = $matches[2][0];
  6. echo <<<Html
  7. <p class="post-images">
  8. <a href="{$this->permalink}" title="{$this->title}">
  9. <img src="{$img}" alt="{$this->title}">
  10. </a>
  11. </p>
  12. Html;
  13. }
  14. ?>
复制代码

边栏不显示博主评论

  1. <?php $this->widget('Widget_Comments_Recent','ignoreAuthor=true')->to($comments); ?>
复制代码

前台登录表单

  1. <form action="<?php $this->options->loginAction()?>" method="post" name="login" rold="form">
  2. <input type="hidden" name="referer" value="<?php $this->options->siteUrl(); ?>">
  3. <input type="text" name="name" autocomplete="username" placeholder="请输入用户名" required/>
  4. <input type="password" name="password" autocomplete="current-password" placeholder="请输入密码" required/>
  5. <button type="submit">登录</button>
  6. </form>
复制代码

......

 赞  赏

如果觉得我的文章对你有用,请随意打赏

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,你说多少就多少

打开  或者  扫一扫,即可进行扫码赞赏哦

原创文章,版权属于:涅槃博客 - love2wind
本文最后更新于2021年09月04日18时47分35秒,已超过1411天没有更新,若内容或图片失效,请留言反馈
本文链接:https://niepan.org/archives/2165.html(转载时请注明本文出处及文章链接)
作品采用:《署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)》许可协议授权

发表评论

博主 - <?php $this->author->screenName(); ?>

love2wind

记录生活,分享世界