typecho文章批评列表经由过程$comments->listComments()函数代码即可挪用,但存在一个问题,就是批评列表的HTML构造是牢固的,限定了文章批评列表款式的设想空间,开发者只能依据其牢固的HTML构造设想批评款式,光荣的是typecho也支撑自定义该函数的HTML代码。下面是转自Typecho官方网站主题开发文档的自定义批评列表地区教程供人人进修参考。
自定义单条批评的HTML代码
在自定义批评前,先设想好单条批评的HTML代码构造,如:
12345678910111213141516 | <li id="li-comment-520" class="comment-body comment-parent comment-odd"><div id="comment-520"><div class="comment-author"><img class="avatar" src="avatar.png" alt="" width="40" height="40"><cite class="fn"><a href="批评者主页">批评者名字</a></cite></div><div class="comment-meta"><a href="批评地点">批评时候</a><span class="comment-reply">复兴按钮</span></div><p>我是批评内容</p></div><!-- 单条批评者信息及内容 --><div class="comment-children"><!-- 嵌套批评相干 --></div></li> |
自定义批评函数
编辑主题的comments.php文件,在文件中第一行增加以下函数代码:
123456789101112131415 | <?php function threadedComments($comments, $options) {$commentClass = '';if ($comments->authorId) {if ($comments->authorId == $comments->ownerId) {$commentClass .= ' comment-by-author'; //如果是文章作者的批评增加 .comment-by-author 款式} else {$commentClass .= ' comment-by-user'; //如果是批评作者的增加 .comment-by-user 款式}} $commentLevelClass = $comments->_levels > 0 ? ' comment-child' : ' comment-parent'; //批评层数大于0为子级,否则是父级?> /* 自定义批评的代码构造 */ <?php } ?> |
把上面自定义单条批评的HTML代码放在自定义批评函数代码解释的处所,以下:
12345678910111213141516171819202122232425262728293031 | <?php function threadedComments($comments, $options) {$commentClass = '';if ($comments->authorId) {if ($comments->authorId == $comments->ownerId) {$commentClass .= ' comment-by-author'; //如果是文章作者的批评增加 .comment-by-author 款式} else {$commentClass .= ' comment-by-user'; //如果是批评作者的增加 .comment-by-user 款式}} $commentLevelClass = $comments->_levels > 0 ? ' comment-child' : ' comment-parent'; //批评层数大于0为子级,否则是父级?> /* 自定义批评的代码构造 */<li id="li-comment-520" class="comment-body comment-parent comment-odd"> <div id="comment-520"> <div class="comment-author"> <img class="avatar" src="avatar.png" alt="" width="40" height="40"> <cite class="fn"><a href="批评者主页">批评者名字</a></cite> </div> <div class="comment-meta"> <a href="批评地点">批评时候</a> <span class="comment-reply">复兴按钮</span> </div> <p>我是批评内容</p> </div><!-- 单条批评者信息及内容 --> <div class="comment-children"> <!-- 嵌套批评相干 --> </div></li> <?php } ?> |
用体系的批评变量替代HTML中相干属性
把HTML里相干的属性,替代成typecho体系中的批评变量,变量的列表能够参考下面。下面的例子,是替代批评的id:
替代前
1 | <li id="li-comment-520" class="comment-body comment-parent comment-odd"> |
替代后
12345678910 | <li id="li-<?php $comments->theId(); ?>" class="comment-body<?php if ($comments->_levels > 0) {echo ' comment-child';$comments->levelsAlt(' comment-level-odd', ' comment-level-even');} else {echo ' comment-parent';}$comments->alt(' comment-odd', ' comment-even');echo $commentClass; ?>"> |
注重:替代ID这里,还需要推断推断当前批评是父级批评照样子级批评,且推断批评 ID 的奇偶数等。
增加嵌套批评(子批评)
替代前:
123 | <div class="comment-children"><!-- 嵌套批评相干 --></div> |
替代后后以下:
12345 | <?php if ($comments->children) { ?> //是不是嵌套批评推断入手下手<div class="comment-children"><?php $comments->threadedComments($options); ?> //嵌套批评一切内容</div><?php } ?> //是不是嵌套批评推断完毕 |
相干变量及申明
1234567 | <?php $comments->gravatar('40', ''); ?> //头像,有两个参数,大小、默许头像?<?php $comments->author(); ?> //批评作者<?php $comments->permalink(); ?> //当前批评的衔接地点<?php $comments->date('Y-m-d H:i'); ?>//批评时候,可在括号里设置花样<?php $comments->reply(); ?> //复兴按钮,可在括号里自定义批评按钮的笔墨<?php $comments->content(); ?> //批评内容<?php $singleCommentOptions->commentStatus(); ?> //初次批评考核提醒 |
终究获得的代码
把上面一切变量都替代完成以后,终究获得的代码以下:
123456789101112131415161718192021222324252627282930313233343536373839404142 | <?php function threadedComments($comments, $options) {$commentClass = '';if ($comments->authorId) {if ($comments->authorId == $comments->ownerId) {$commentClass .= ' comment-by-author';} else {$commentClass .= ' comment-by-user';}} $commentLevelClass = $comments->levels > 0 ? ' comment-child' : ' comment-parent';?> <li id="li-<?php $comments->theId(); ?>" class="comment-body<?php if ($comments->levels > 0) {echo ' comment-child';$comments->levelsAlt(' comment-level-odd', ' comment-level-even');} else {echo ' comment-parent';}$comments->alt(' comment-odd', ' comment-even');echo $commentClass;?>"> <div id="<?php $comments->theId(); ?>"> <div class="comment-author"><?php $comments->gravatar('40', ''); ?><cite class="fn"><?php $comments->author(); ?></cite> </div> <div class="comment-meta"> <a href="<?php $comments->permalink(); ?>"><?php $comments->date('Y-m-d H:i'); ?></a> <span class="comment-reply"><?php $comments->reply(); ?></span> </div><?php $comments->content(); ?><?php $singleCommentOptions->commentStatus(); ?></div><?php if ($comments->children) { ?><div class="comment-children"><?php $comments->threadedComments($options); ?></div><?php } ?></li><?php } ?> |
至此,<?php $comments->listComments(); ?>函数的HTML构造就变成上面自定义的HTML构造了。
注重:上面的自定义批评代码输出的,就是原本批评页里的下面这段代码,所以不要删除或修正这段代码。
1 | <?php $comments->listComments(); ?> |
补充:别的批评变量
123456789 | <?php $comments->theId(); ?> //每一个批评的唯一ID<?php $comments->sequence(); ?> //批评地点楼层<?php $comments->responseUrl(); ?> //复兴地点<?php $comments->responseId(); ?> //复兴框ID<?php $comments->trackbackUrl(); ?> //trackback地点<?php $comments->author(); ?> //批评者的名字<?php $comments->date('F jS, Y'); ?> //批评日期<?php $comments->date('h:i a'); ?> //批评时候<?php $comments->content(); ?> //批评内容 |
原文地点:http://docs.typecho.org/themes/custom-comments

评论列表