蚂蚱发表在使用中的自定义 Typecho 的评论列表
把以下代码放在 comments.php
假如我们要实现一段这样的评论的 html 代码,效果当然要综合 css 样式来看
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| <li id="li-comment-250" class="comment-body comment-parent comment-odd"> <div id="comment-250"> <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 文件,做好修改准备。
一、把上面的代码,也即你要自定义的单条评论的 html 代码,复制到这个文件的最上面
二、在这段代码的外围加入自定义评论样式的函数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| <?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'; ?>
<?php } ?>
|
三、在 li 的 css 样式中判断不同层次的、不同 ID 的评论,并添加 css 样式
注意区别下面两句的不同地方
1
| <li id="li-comment-250" class="comment-body comment-parent comment-odd">
|
加入判断代码之后,第一处是判断评论 ID,后者是判断当前评论是父级评论还是子级评论,且判断评论 ID 的奇偶数
1 2 3 4 5 6 7 8 9 10
| <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; ?>">
|
四、添加评论者的相关信息和评论内容
1 2 3 4 5 6 7 8 9 10 11
| <div id="comment-250"> <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>
|
更改后代码,注意各个信息的代码是什么
1 2 3 4 5 6 7 8 9 10 11
| <div id="<?php $comments->theId(); ?>"> <!-- 评论 ID --> <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(); ?> <!-- 评论内容 --> </div>
|
相关参数及说明:
1 2 3 4 5 6
| <?php $comments->gravatar('40', ''); ?> <?php $comments->author(); ?> <?php $comments->permalink(); ?> <?php $comments->date('Y-m-d H:i'); ?> <?php $comments->reply(); ?> <?php $comments->content(); ?>
|
五、添加嵌套评论(子评论)的代码
1 2 3 4 5
| <?php if ($comments->children) { ?> <div class="comment-children"> <?php $comments->threadedComments($options); ?> //嵌套评论所有内容 </div> <?php } ?> //是否有嵌套评论判断结束
|
更改后如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| <?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(); ?> </div> <?php if ($comments->children) { ?> <div class="comment-children"> <?php $comments->threadedComments($options); ?> </div> <?php } ?> </li> <?php } ?>
|
到这里整个自定义评论样式到这里就结束了,最终得到的便是以下代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| <?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(); ?> </div> <?php if ($comments->children) { ?> <div class="comment-children"> <?php $comments->threadedComments($options); ?> </div> <?php } ?> </li> <?php } ?>
|
注意:上面的自定义评论代码输出的就是本来评论页里的这段代码,所以你就不用对原来的这段代码做任何更改了。
1
| <?php $comments->listComments(); ?>
|
SVN 版本更新:首次评论审核提示,在自定义评论代码的适当地方添加以下语句,否则将看不到审核提示语句。
1 2 3
| <?php if ('waiting' == $this->status) { ?> <em class="awaiting"><?php $singleCommentOptions->commentStatus(); ?></em> //$singleCommentOptions 对应函数的第二个变量,如这里是 $options <?php } ?>
|
蚂蚱整理于 2012.03.16
当然最后你还得定制你的 CSS 样式,这才是关系到你评论的展示效果至关重要的。同样的 HTML 布局,不同 CSS 有不同的效果。
Relay Tips: 一极乐( https://yijile.com/log/158/ )