介绍

默认的typecho主题只能新增文章,最近使用就在想能不能加个心情的页面类似于说说,可以发一些自己的心情之类的,并且对其他文章没有影响,首页也不展示。

研究了一番,发现网上也没有很好的实现方式,有的插件能够实现样式也与主题不协调,所有就自己搞了一个出来。作为PHP小白,可能搞得不是很好。如有其他想法或建议欢迎在评论区分享!

预览:碎语心情

原理

其实原理很简单,只要新增一个分类为碎语心情,然后在首页和最近文章中删除该分类下的文章即可

步骤

1新增碎语心情分类

image-20250317162529935

2首页显示过滤掉该分类

由于typecho没有该功能,可以使用CateFilter插件实现,官网版本太低,可以使用二改的适配typecho1.2

github:https://github.com/typecho-fans/plugins/tree/master/CateFilter

下载后复制到plugins文件夹下后开启即可

image-20250317164141699

这里填的是分类id

3自定义模板文件

使用typecho自带的独立页面功能,新增一个只展示碎语心情分类下文章的独立页面

3.1在使用的主题文件夹中新增page-mood.php

<?php 
/**
 * 碎语心情
 * @author WQG www.wqg.life
 * @package custom
 */

if (!defined('__TYPECHO_ROOT_DIR__')) exit; ?>
<?php $this->need('header.php'); ?>


<?php $this->widget('Widget_Archive@myCustomCategory', 'type=category', 'mid=7')->to($categoryPosts); ?>
<div class="col-mb-12 col-8" id="main" role="main">
    <?php if ($categoryPosts->have()): ?>
        <?php while ($categoryPosts->next()): ?>
            <article class="post" itemscope itemtype="http://schema.org/BlogPosting">
                <h2 class="post-title" itemprop="name headline"><a itemprop="url"
                                                                   href="<?php $categoryPosts->permalink() ?>"><?php $categoryPosts->title() ?></a>
                </h2>
                <div class="post-content" itemprop="articleBody">
                    <?php $categoryPosts->content('- 阅读剩余部分 -'); ?>
                </div>
                <ul class="post-meta">
                    <li itemprop="author" itemscope itemtype="http://schema.org/Person"><?php _e('BY: '); ?><a
                            itemprop="name" href="<?php $categoryPosts->author->permalink(); ?>"
                            rel="author"><?php $categoryPosts->author(); ?></a></li>
                    <li><?php _e('时间: '); ?>
                        <time datetime="<?php $categoryPosts->date('c'); ?>"
                              itemprop="datePublished"><?php $categoryPosts->date(); ?></time>
                    </li>
                    <li itemprop="interactionCount"><a
                            href="<?php $categoryPosts->permalink() ?>#comments"><?php $categoryPosts->commentsNum('评论', '1 条评论', '%d 条评论'); ?></a>
                    </li>
                </ul>
            </article>
        <?php endwhile; ?>
    <?php else: ?>
        <article class="post">
            <h2 class="post-title"><?php _e('没有找到内容'); ?></h2>
        </article>
    <?php endif; ?>

    <?php $categoryPosts->pageNav('&laquo; 前一页', '后一页 &raquo;'); ?>
</div><!-- end #main -->


<?php $this->need('sidebar.php'); ?>
<?php $this->need('footer.php'); ?>

为了适配主题风格,具体样式可参考主题内archive.php文件

3.2新建独立页面使用模板

image-20250317164951595

至此博客已经能够显示碎语心情tab并且风格样式均与主题一致

image-20250317165140198

4最新文章中过滤掉碎语心情分类下的文章

我是不希望碎语心情不展示在最新文章中,如果想展示,此步可以滤过。

网上没有找到这个插件,因此可以直接修改源码实现,php文件位置为var/Widget/Contents/Post/Recent.php

<?php

namespace Widget\Contents\Post;

use Typecho\Db;
use Widget\Base\Contents;

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

/**
 * 最新评论组件
 *
 * @category typecho
 * @package Widget
 * @copyright Copyright (c) 2008 Typecho team (http://www.typecho.org)
 * @license GNU General Public License 2.0
 */
class Recent extends Contents
{
    /**
     * 执行函数
     *
     * @throws Db\Exception
     */
    public function execute()
    {
        $this->parameter->setDefault(['pageSize' => $this->options->postsListSize]);

        $this->db->fetchAll($this->select()
            ->join('table.relationships', 'table.contents.cid = table.relationships.cid')
            ->where('table.relationships.mid != ?', 7)
            ->where('table.contents.status = ?', 'publish')
            ->where('table.contents.created < ?', $this->options->time)
            ->where('table.contents.type = ?', 'post')
            ->order('table.contents.created', Db::SORT_DESC)
            ->group('cid')
            ->limit($this->parameter->pageSize), [$this, 'push']);
    }
}

原理也很简单,在之前的基础上连接了一下关系表,排除掉碎语心情分类下的文章

->where('table.relationships.mid != ?', 7)

这里的7改为自己的即可

总结

优点:

  • 不该表原有表结构
  • 本质还是原有文章,支持所有和文章相同的功能,支持加密,评论等功能

缺点

  • 需要修改源码,改的地方比较多,迁移比较麻烦

标签: typecho

添加新评论