因为看到很多博客的个人中心都有文章和评论,刚好本站也有个人中心,于是也想用typecho实现,百度找到了参考文章。

多次测试使用下来发现了个缺点,函数是根据邮箱获取的评论,如果用户更改了邮箱,之前使用原邮箱的评论就不会显示。甚至会出现作者A修改成作者B的邮箱,输出作者B的评论。

于是开始琢磨,进入数据库查看,发现comments表里是有authorId字段的,也就是说是可以实现根据作者id输出评论的,这样的好处就是不用理会作者的邮箱。不会出现以上说出的缺点。实现起来也比较容易。
实现方法

/*输出作者发表的评论*/
class Widget_Post_AuthorComment extends Widget_Abstract_Comments
{
    public function execute()
    {
        global $AuthorCommentId;//全局作者id
        $select  = $this->select()->limit($this->parameter->pageSize)
        ->where('table.comments.status = ?', 'approved')
        ->where('table.comments.authorId = ?', $AuthorCommentId)//获取作者id
        ->where('table.comments.type = ?', 'comment')
        ->order('table.comments.coid', Typecho_Db::SORT_DESC);//根据coid排序
        $this->db->fetchAll($select, array($this, 'push'));
    }
}

使用方法

<?php
global $AuthorCommentId;//全局作者id
$AuthorCommentId=$this->authorId;//获取作者id
?>
<?php $this->widget('Widget_Post_AuthorComment@index','pageSize=15')->to($AuthorComment); ?>
<?php while($AuthorComment->next()): ?>
//这里是代码
<li><a href="<?php $AuthorComment->permalink() ?>"><?php $AuthorComment->excerpt(50, '...'); ?></a></li>
<?php endwhile; ?>

扫描二维码,在手机上阅读!