Typecho Wiki
每一个作品都值得被记录

Typecho 获取指定用户的评论列表

Typecho维基君Typecho教程 • 2796次浏览 • 发布 2018-07-11 • 更新 2022-10-13
极致加速的V2Ray 助您畅享全球网络 & 搬瓦工VPS最新优惠码
🛜自用大流量超低月租手机卡推荐榜单 #拒绝流量焦虑

typecho中,可以获取最新的评论列表,那么如果我只想获取某个人的评论列表,例如作者的评论,怎么实现?今天TypechoWiki给大家带来方法。

解决方案

typecho自带的评论组件不包含自定义作者的功能。你可以自行扩展,下面给出详细代码。

在你的主题的functions.php中加入以下代码,以默认主题default为例:

class Widget_Comments_RecentPlus extends Widget_Abstract_Comments
{
    /**
     * 构造函数,初始化组件
     *
     * @access public
     * @param mixed $request request对象
     * @param mixed $response response对象
     * @param mixed $params 参数列表
     * @return void
     */
    public function __construct($request, $response, $params = NULL)
    {
        parent::__construct($request, $response, $params);
        $this->parameter->setDefault(array('pageSize' => $this->options->commentsListSize, 'parentId' => 0, 'ignoreAuthor' => false));
    }

    /**
     * 执行函数
     *
     * @access public
     * @return void
     */
    public function execute()
    {
        $select  = $this->select()->limit($this->parameter->pageSize)
        ->where('table.comments.status = ?', 'approved')
        ->order('table.comments.coid', Typecho_Db::SORT_DESC);

        if ($this->parameter->parentId) {
            $select->where('cid = ?', $this->parameter->parentId);
        }

        if ($this->options->commentsShowCommentOnly) {
            $select->where('type = ?', 'comment');
        }
        
        /** 忽略作者评论 */
        if ($this->parameter->ignoreAuthor) {
            $select->where('ownerId <> authorId');
        }

        if ($this->parameter->mail) {
            $select->where('mail = ?', $this->parameter->mail);
        }

        $this->db->fetchAll($select, array($this, 'push'));
    }
}

在sidebar.php中修改对应内容如下,其中test@qq .com就是你对应的需要显示的人的邮箱,注意是Widget_Comments_RecentPlus不是Widget_Comments_Recent

<section class="widget">
   <h3 class="widget-title"><?php _e('最近回复'); ?></h3>
   <ul class="widget-list">
   <?php $this->widget('Widget_Comments_RecentPlus', 'mail=test@qq.com')->to($comments); ?>
   <?php while($comments->next()): ?>
      <li><a href="<?php $comments->permalink(); ?>"><?php $comments->author(false); ?></a>: <?php $comments->excerpt(35, '...'); ?></li>
   <?php endwhile; ?>
   </ul>
</section>

广告声明:文内含有的对外跳转链接(包括不限于超链接、二维码、口令等形式),用于传递更多信息,节省甄选时间,结果仅供参考,Typecho.Wiki所有文章均包含本声明。
本文检索关键词:typecho
厂商投放

【腾讯云】🎉五一云上盛惠!云服务器99元/月续费同价!

腾讯云五一劳动节海量产品 · 轻松上云!云服务器首年1.8折起,买1年送3个月!超值优惠,性能稳定,让您的云端之旅更加畅享。快来腾讯云选购吧!

广告
添加新评论 »

已有 3 条评论 »

  1. 可以自定义test@qq .com这个邮箱么,我用获取到登录用户的邮箱,然后赋值到里面类似mail=$admin,但是一直是失效状态,换成mail=test@qq .com的格式就可以使用,不知道可否解答下,麻烦啦~

    1. 看了你的博客原来你是要本地留言用户的邮箱,这个只能通过cookie读取了

    2. 建议直接从数据读取评论表,然后过滤你想要显示的用户评论信息