现在的主题的评论有两种方法显示回复评论,一种是嵌套评论,一层评论套一层评论回复。一种是@评论,就是以@+评论人名的方式回复。其实两者都有优缺点,前者的优点就是清楚的看出回复所针对的内容!缺点是极大的破坏了评论的显示布局。后者是充分的保证了评论的显示布局,使之主题更佳美观!缺点是如果多评论的话,不容易让人了解究竟回复的是什么内容。 接下来我们来讲讲怎么嵌套评论!
在 WordPress 2.7 之前,要实现嵌套评论,我们必须开启 WordPress Thread Comment这个插件,而在即将发布的 WordPress 2.7 中,Thread Comment 将被集成进来,使用 wp_list_comments函数即可调用嵌套评论(Thread Comment)。也想使自己的主题支持嵌套评论(Thread Comment)这个功能?那就继续看下去吧。
1.添加 comment-reply JavaScript
让评论嵌套 (Thread comment) 能够正常运行在 wp_head() 函数之前添加如下函数:
1 | < ?php if(is_singular()) wp_enqueue_script( 'comment-reply' ); ?> |
其作用是加载嵌套回复所需的 JavaScript 代码. (也就是说, 如果浏览器不支持 JavaScript, 嵌套回复就没法实现)
2.判断 WordPress 版本
有选择性地使用 wp_list_comments 函数WordPress 2.7 通过函数 wp_list_comments 来显示所有留言,可之前版本并没有这个函数,所以,我们需在 comments.php 文件中添加如下代码:
1 2 3 4 | < ?php if (function_exists('wp_list_comments')) : ?> // new comments.php stuff ; < ?php else : ?> // old comments.php stuff endif; |
3.WordPress 2.7 的评论 Loop
1 2 3 4 5 6 7 8 9 10 11 12 13 | < ?php if ($comments) : ?> <span>< ?php comments_number('No Comments', 'One Comment', '% Comments' ); ?></span> <div class="comment_list"> < ?php wp_list_comments(); ?> < ?php else : // this is displayed if there are no comments so far ?> < ?php if ('open' == $post->comment_status) : ?> <!-- If comments are open, but there are no comments. --> < ?php else : // comments are closed ?> <!-- If comments are closed. --> <p class="nocomments">Comments are closed.</p> < ?php endif; ?> < ?php endif; ?> </div> |
4.设置评论框
需要要把评论框 (Comment Form) 放入一个 ID 为 respond 的 DIV 中,然后并在评论框中添加如下代码:
1 | < ?php comment_id_fields(); ?> |
当然,我们也可以取消回复
1 2 3 | <div class="cancel-comment-reply"> <small>< ?php cancel_comment_reply_link(); ?></small> </div> |
接下来就是css部分的美化了,还有就是要wordpress后台讨论中选择支持嵌套评论,不然搞了这么多也不会有嵌套评论出现的!

对《使主题支持嵌套评论》评论