typecho留言加上@

唔,这个功能其实以前在用em的时候还不怎么喜欢……现在越来越发现他的好处了。
尤其是使用嵌套,一层里好多人回复的时候……根本不知道你这条留言是给哪位的回复orz有了@功能就会明白很多啦!
em的评论是自带这个功能的也没有想过要怎么去实现,然而typecho是木有的……所以我就只好摸摸头自己去搜了。
然后我就搜索到了!!!感谢各位大神给我等小白提供给了便利!


首先打开模板文件夹里的functions.php文件,添加以下代码。

/*评论增加@功能*/
function getPermalinkFromCoid($coid) {   
$db       = Typecho_Db::get();   
$options  = Typecho_Widget::widget('Widget_Options');   
$contents = Typecho_Widget::widget('Widget_Abstract_Contents');   
$row = $db->fetchRow($db->select('cid, type, author, text')->from('table.comments')   
          ->where('coid = ? AND status = ?', $coid, 'approved'));   
if (empty($row)) return 'Comment not found!';   
$cid = $row['cid'];   
$select = $db->select('coid, parent')->from('table.comments')   
          ->where('cid = ? AND status = ?', $cid, 'approved')->order('coid');   
if ($options->commentsShowCommentOnly)   
    $select->where('type = ?', 'comment');   
$comments = $db->fetchAll($select);   
if ($options->commentsOrder == 'DESC')   
    $comments = array_reverse($comments);   
foreach ($comments as $key => $val)   
    $array[$val['coid']] = $val['parent'];   
$i = $coid;   
while ($i != 0) {   
    $break = $i;   
    $i = $array[$i];   
}   
$count = 0;   
foreach ($array as $key => $val) {   
    if ($val == 0) $count++;    
    if ($key == $break) break;    
}   
$parentContent = $contents->push($db->fetchRow($contents->select()->where('table.contents.cid = ?', $cid)));   
$permalink = rtrim($parentContent['permalink'], '/');   
$page = ($options->commentsPageBreak)   
      ? '/comment-page-' . ceil($count / $options->commentsPageSize)   
      : ( substr($permalink, -5, 5) == '.html' ? '' : '/' );   
return array(   
    "author" => $row['author'],   
    "text" => $row['text'],   
    "href" => "{$permalink}{$page}#{$row['type']}-{$coid}"  
);   
}  

然后打开你的comment.php文件

<?php $comments->content(); ?>

首先先找到上面这行代码,这是显示评论正文的,一般会选择在正文的上面添加显示@author的代码。
PS 如果在comment.php文件里找不到这段代码,请先参考typecho文档的“自定义评论列表区域”进行修改。

修改完后大概样子如下:

<?php  
if($comments->parent){   
    $p_comment = getPermalinkFromCoid($comments->parent);   
    $p_author = $p_comment['author'];   
    $p_text = mb_strimwidth(strip_tags($p_comment['text']), 0, 100,"...");   
    $p_href = $p_comment['href'];   
    echo "<span>@</span><a href='$p_href' title='$p_text'>$p_author</a>";   
}   
?>  
<?php $comments->content(); ?>

这一段代码以后就可以了,具体样式可以去评论区看看啦。
当然也可以根据自己喜好换位置调整下排版什么的。
于是最终效果完成了!

代码引用自:WordPress & Typecho 评论回复