[教程] typecho获取当前作者的全部评论

查看: 861|回复: 0
crll 发表于 2023-4-6 15:46:56
因为看到很多博客的个人中心都有文章和评论,刚好本站也有个人中心,于是也想用typecho实现,百度找到了参考文章。

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

于是开始琢磨,进入数据库查看,发现comments表里是有authorId字段的,也就是说是可以实现根据作者id输出评论的,这样的好处就是不用理会作者的邮箱。不会出现以上说出的缺点。实现起来也比较容易。
实现方法
  1. /*输出作者发表的评论*/
  2. class Widget_Post_AuthorComment extends Widget_Abstract_Comments
  3. {
  4.     public function execute()
  5.     {
  6.         global $AuthorCommentId;//全局作者id
  7.         $select  = $this->select()->limit($this->parameter->pageSize)
  8.         ->where('table.comments.status = ?', 'approved')
  9.         ->where('table.comments.authorId = ?', $AuthorCommentId)//获取作者id
  10.         ->where('table.comments.type = ?', 'comment')
  11.         ->order('table.comments.coid', Typecho_Db::SORT_DESC);//根据coid排序
  12.         $this->db->fetchAll($select, array($this, 'push'));
  13.     }
  14. }
复制代码

使用方法
  1. <?php
  2. global $AuthorCommentId;//全局作者id
  3. $AuthorCommentId=$this->authorId;//获取作者id
  4. ?>
  5. <?php $this->widget('Widget_Post_AuthorComment@index','pageSize=15')->to($AuthorComment); ?>
  6. <?php while($AuthorComment->next()): ?>
  7. //这里是代码
  8. <li><a href="<?php $AuthorComment->permalink() ?>"><?php $AuthorComment->excerpt(50, '...'); ?></a></li>
  9. <?php endwhile; ?>
复制代码
博主论坛 bzlt.net
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关于本站联系我们FAQ友情链接免责声明生存法则

Build with for "make" Copyright © 2020-2022. Powered by Discuz! GMT+8, 2024-3-28 20:31

快速回复 返回顶部 返回列表