惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

TaoSecurity Blog
TaoSecurity Blog
Jina AI
Jina AI
雷峰网
雷峰网
月光博客
月光博客
The GitHub Blog
The GitHub Blog
WordPress大学
WordPress大学
B
Blog RSS Feed
美团技术团队
C
CXSECURITY Database RSS Feed - CXSecurity.com
小众软件
小众软件
Security Latest
Security Latest
Microsoft Azure Blog
Microsoft Azure Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
C
Cybersecurity and Infrastructure Security Agency CISA
Last Week in AI
Last Week in AI
A
Arctic Wolf
Latest news
Latest news
Attack and Defense Labs
Attack and Defense Labs
I
Intezer
F
Fortinet All Blogs
罗磊的独立博客
MongoDB | Blog
MongoDB | Blog
Webroot Blog
Webroot Blog
S
Secure Thoughts
Help Net Security
Help Net Security
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
V
Visual Studio Blog
P
Proofpoint News Feed
博客园 - 【当耐特】
P
Privacy International News Feed
V
Vulnerabilities – Threatpost
Stack Overflow Blog
Stack Overflow Blog
Know Your Adversary
Know Your Adversary
云风的 BLOG
云风的 BLOG
Hacker News: Ask HN
Hacker News: Ask HN
L
LINUX DO - 最新话题
H
Help Net Security
爱范儿
爱范儿
酷 壳 – CoolShell
酷 壳 – CoolShell
S
SegmentFault 最新的问题
Forbes - Security
Forbes - Security
T
Tailwind CSS Blog
量子位
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
T
Tenable Blog
Cloudbric
Cloudbric
N
News and Events Feed by Topic
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Hugging Face - Blog
Hugging Face - Blog

洛丽糖

"微博客"的重生与否之相关历程 小小甲虫拿捏 月季从盛放至凋零 “微博客”程序正式停止维护更新了 救命!这串李子看得我口水狂飙! 山野桃林闲游 枝头青桃,满是初夏的清甜念想 从别人家路过拍的,不知道叫什么? 一颗桃子的一生:从青涩到“爆甜”的旅程 田地变虾塘,农村人的前路该往哪走 p:last-child 与 p:last-of-type 的核心区别(AI教程版) 雪豹速清app 很久之前拍的:青黄小樱桃,酸甜正撩人 “XmBooks”主题更新日志动态 网站 SSL 证书过期,火速续上免费三月证书 本站也实现了走心评论精选功能 老家插秧季,一田新绿入夏来 稻田里撞见,一场新生的蜕变
Typecho走心评论精选功能实现代码教程
寻梦xunm · 2026-05-03 · via 洛丽糖
首先声明

代码并不是唯一,实现方式也不是唯一,这里博主只提供一种简单粗暴的教程思路。如果有更好可以方案就不需要查看此篇教程,至于代码效率,安全等问题不在下面的教程考虑范围内,只要能跑就行。好了,下面正式开始教程。

第一步:添加API接口地址(博主这里由于不喜欢使用异步加载数据就没有使用json格式数据传输)
function themeInit($archive){
$route = $archive->request->getPathInfo();
if (strpos($route, '/api/') !== false) {
        switch ($route) {

            case '/api/zx':

                zx($archive);

            break;

            case '/api/sx':

                sx($archive);

            break;
        }

}
}
第二步:添加走心评论精选操作功能(其实这里可以合并成一个函数,可以节省一部分重复查询问题)

1.这个函数主要用于给评论添加走心精选功能

/* 评论走心处理 */
function zx($archive){
    //评论id
    $coid = (int) $_GET['coid'];

    if (!$coid) {
        echo '<script>
            alert("评论ID无效,请刷新页面重试!");
            window.history.back(); // 返回上一页
          </script>';
        return;
    }
    
    
    //判断是否为登录 true 为已经登录
    $loginState = false;
    if (Typecho_Widget::widget('Widget_User')->hasLogin() && Typecho_Widget::widget('Widget_User')->pass('administrator')) {
        $loginState = true;
    }
    if (!$loginState) {
        echo '<script>
            alert("请先登录");
            window.history.back(); // 返回上一页
          </script>';
        return;
    }
    $db = Typecho_Db::get();
    if (!array_key_exists('zouxin', $data = $db->fetchRow($db->select()->from('table.comments')->where('coid = ?', $coid)))) {
        //  在文章表中创建一个字段用来存储点赞数量
        $db->query('ALTER TABLE `' . $db->getPrefix() . 'comments` ADD `zouxin` INT(10) NOT NULL DEFAULT 0;');
    }

    $updateRows = $db->query($db->update('table.comments')->rows(array('zouxin' => 1))->where('coid = ?', $coid));
    
    if($updateRows){
            echo '<script>
                alert("设为走心成功");
                window.history.back(); // 返回上一页
              </script>';
        }else{
            echo '<script>
                alert("设为走心失败");
                window.history.back(); // 返回上一页
              </script>';
        }

}

2.这个函数主要用于给评论取消走心精选功能

/* 评论走心处理 */
function sx($archive){
    //评论id
    $coid = (int) $_GET['coid'];

    if (!$coid) {
        echo '<script>
            alert("评论ID无效,请刷新页面重试!");
            window.history.back(); // 返回上一页
          </script>';
        return;
    }
    
    
    //判断是否为登录 true 为已经登录
    $loginState = false;
    if (Typecho_Widget::widget('Widget_User')->hasLogin() && Typecho_Widget::widget('Widget_User')->pass('administrator')) {
        $loginState = true;
    }
    if (!$loginState) {
        echo '<script>
            alert("请先登录");
            window.history.back(); // 返回上一页
          </script>';
        return;
    }
    $db = Typecho_Db::get();
    if (!array_key_exists('zouxin', $data = $db->fetchRow($db->select()->from('table.comments')->where('coid = ?', $coid)))) {
        //  在文章表中创建一个字段用来存储点赞数量
        $db->query('ALTER TABLE `' . $db->getPrefix() . 'comments` ADD `zouxin` INT(10) NOT NULL DEFAULT 0;');
    }

    $updateRows = $db->query($db->update('table.comments')->rows(array('zouxin' => 0))->where('coid = ?', $coid));
    
    if($updateRows){
            echo '<script>
                alert("取消走心成功");
                window.history.back(); // 返回上一页
              </script>';
        }else{
            echo '<script>
                alert("取消走心失败");
                window.history.back(); // 返回上一页
              </script>';
        }

}
第三步:在评论文件中添加相关操作了链接,重要提示:这一步需要自定义评论才可以使用,如果这就使用官方的默认评论格式无法添加下面的代码。下面的代码自己随便在自定义评论函数当中任意位置添加。(注意:博主这里实在不知道要用什么代码获取相关URL,就直接把URL写死了,如果在实际环境中出现url地址问题,可以自行更具提示调整)
<?php if (Helper::options()->zouxin && Typecho_Widget::widget('Widget_User')->hasLogin() && Typecho_Widget::widget('Widget_User')->pass('administrator')): ?>
                <span class="fg"> . </span>
                <?php if ($comments->zouxin == 1): ?>
                <a href="<?php Helper::options()->index("/api/sx?coid=$comments->coid"); ?>" class="btn btn-xs btn-info">取消走心</a>
                <?php else: ?>
                <a href="<?php Helper::options()->index("/api/zx?coid=$comments->coid"); ?>" class="btn btn-xs btn-success">设为走心</a>
                <?php endif; ?>
                <?php endif; ?>
第四步:自定义一个独立页面模板,然后后台创建独立页面选择你相应的模板即可。(重要提示:这里博主也不会使用官方自带的分页代码,索性就不使用,反正无伤大雅。)

此处内容需要评论 回复 后方可阅读。