WordPress 2.7 Separate 留言和样式化留言
2008年11月03日
上次我讲了如何让你的主题适合 WordPress 2.7 Thread Comments,但是默认实现的 Thread Comments 无法定义 Comments 的格式,也没有把 Trackbacks 分开,并且 Trackbacks 也可以回复,非常无语,那么今天我就讲讲如果,把留言分开成 Comments 和 Trackbacks,和样式化留言。
1. 首先修改你主题的 single.php 文件。把
<?php comments_template(); ?>
函数替换成
<?php comments_template('', true); ?>
上面的修改经让 comments_template 函数创建一个$comments_by_type 的变量。后面我们将会用到。
2. 打开 comments.php 文件,把
<ul class="commentlist"> <?php wp_list_comments(); ?> </ul>;
替换成
<?php if ( ! empty($comments_by_type['comment']) ) : ?> <ul class="commentlist"> <?php wp_list_comments(array ('type' => 'comment'); ?> </ul> <?php endif; ?>
上面这段代码通过指定 type 为 comments 来实现显示的留言都是 comments.
3. 然后在下面添加:
<?php if ( ! empty($comments_by_type['pings']) ) : ?><h2 id="pingback">Trackbacks/Pingbacks</h2><ul class="commentlist"> <?php wp_list_comments(array ('type' => 'pings')); ?></ul><?php endif; ?>
上面这段代码通过指定 type 为 pings 来实现现实的留言都是 Trackbacks.
4. 通过上面三个步骤,已经把留言分成了 comments 和 Trackbacks,但是几个问题依然存在, Trackbacks 竟然也可以回复,Comments 列表不能适应你原有的主题的样式。这个时候我们可以使用 callback 参数来指定一个函数定义留言列表的样式。打开你主题的 functions.php 函数,添加如下两个函数:
5. 自定义 Trackbacks 列表函数。
<?php //定义 Trackbacks 列表 function custom_pings($comment, $args, $depth) { $GLOBALS['comment'] = $comment; ?> <li id="comment-<?php comment_ID( ); ?>" class="trackback"><strong></strong><?php comment_author_link(); ?> </li> <?}?>
6. 自定义 Comments 列表函数
<?php function custom_comments($comment, $args, $depth) { $GLOBALS['comment'] = $comment; global $commentcount; if(!$commentcount) $commentcount = 0; $commentcount ++; global $commentalt; ($commentalt == "alt")?$commentalt="":$commentalt="alt"; ?> <li <?php comment_class(); ?> id="comment-<?php comment_ID(); ?>"> <p class="header <?php echo $commentalt?>"><strong><?php echo $commentcount ?>.</strong> <?php comment_author_link( ); ?> | <?php comment_date( ); ?> at <?php comment_time(); ?> | <a href="#comment-<?php comment_ID( ); ?>">#</a></p> <?php echo get_avatar( $comment, 32 ); ?> <?php comment_text( ); ?> <?php edit_comment_link('Edit Comment','<span class="editlink">','</span>'); ?> <?php comment_reply_link(array('depth' => $depth, 'max_depth'=> $args['max_depth'], 'reply_text' => "回复该留言"));?> </li> <?php } ?>
7. 再次打开 comments.php 文件,把下面两个函数修改为:
<?php wp_list_comments(array ('type' => 'comment')); ?> <?php wp_list_comments(array ('type' => 'pings')); ?>
修改为:
<?php wp_list_comments(array ('type' => 'comment','callback' => 'custom_comments')); ?> <?php wp_list_comments(array ('type' => 'pings','callback' => 'custom_pings')); ?>
35 条评论 添加你的评论 →

1. sofish | 2008:11:03 - 12:52:09 | #
终于可以复制了,哈哈,这篇文章就有用多了。
…
2. 雀巢 | 2008:11:03 - 12:52:50 | #
慢了一步
3. 颜小诗 | 2008:12:12 - 16:42:28 | #
还是搞不定。。。貌似有点不明白。。。额
4. Denis | 2008:11:03 - 13:01:04 | #
难道我还有文章那个没用的?
5. Denis | 2008:11:03 - 17:17:40 | #
测试留言回复email通知。
6. Denis | 2008:11:03 - 17:20:34 | #
在测试下!
7. Denis | 2008:11:03 - 17:25:40 | #
这次测试应该会成功了吧?
8. Denis | 2008:11:03 - 17:31:37 | #
Good,留言回复 Email 通知成功。
9. 辐射鱼 | 2008:11:03 - 13:42:20 | #
你动作好快
10. 雀巢 | 2008:11:03 - 12:52:11 | #
沙发先
11. sofish | 2008:11:03 - 12:52:52 | #
扯平…….扯平,我最喜欢这种结果了.
12. Dianso | 2008:11:03 - 13:09:39 | #
13. David | 2008:11:04 - 11:36:08 | #
测试下
14. 时 | 2008:11:03 - 13:32:06 | #
这个一定要顶。。
15. 时 | 2008:11:03 - 13:34:03 | #
我的留言哪里去了?
16. dupola | 2008:11:03 - 14:05:29 | #
很好很强大。我正需要。
谢谢 。
17. 老所 | 2008:11:03 - 14:47:34 | #
先收藏,等换2.7后再仔细看:)
18. 狐狸 | 2008:11:03 - 15:15:37 | #
先加个星号,等着2.7正式版
19. MY-Hou | 2008:11:03 - 16:14:40 | #
先本地试下…2.7…哇塞…吼吼
20. yeardo | 2008:11:03 - 17:40:38 | #
老大,你用的啥编辑器哟……
高亮颜色不错哟……
21. Denis | 2008:11:03 - 17:43:08 | #
Coolcode 并且帮我看看能否收到回复的 email。
22. volgus | 2008:11:03 - 19:11:52 | #
这篇文章的效果就是现在这样可以回复留言吗!
23. 黑色饼干 | 2008:11:03 - 20:28:02 | #
期待2.7的发布啊
24. jamesben | 2008:11:03 - 22:20:27 | #
你的存档页面是怎么实现的呢?
25. gowers | 2008:11:04 - 10:20:08 | #
呵呵,有点意思~~
26. 偶爱偶家 | 2008:11:06 - 08:05:03 | #
水煮鱼已经用上2.7了呀, 恭喜恭喜, 不过这样修改主题好像并不是最好吧? 应该是自定义一个类好像不错.
27. Denis | 2008:11:06 - 10:44:36 | #
恩,不会类,太累了。
28. rain | 2008:11:11 - 20:08:27 | #
期待正式版的发布在升级啦!
29. fooldy | 2008:12:12 - 14:41:21 | #
我的comments.php 都没有wp_list_comments
函数?
30. ray1980 | 2008:12:31 - 16:37:26 | #
我的出现这个错误:
Warning: Cannot modify header information – headers already sent by (output started at /domains/koryi.net/wp-content/themes/black/functions.php:11
第十一行是这个:
<?php function custom_pings($comment, $args, $depth) {
31. aoao | 2009:01:11 - 04:28:34 | #
6. 自定义 Comments 列表函数
最后应该不用。。我刚才发现产生结构不对。。查了一下官方发现的
32. Shane | 2009:01:26 - 19:58:08 | #
最近在試如何讓他人回覆的圖像顯示
找到這幾篇文章
只是要修改的地方可真多
33. 炜煜 | 2009:03:10 - 19:49:40 | #
感谢好文章,我按照您的方法修改了之后,确实如愿了。不过我给主题下的functions.php添加了2个函数,结果就造成只要一有数据提交,就页面空白的问题。不管前台后台都一样。哪位大侠帮忙看看我的functions.php文件是不是哪里写错了?functions.php地址:
http://9npc.com/wp-content/themes/PureCSS/functions.php.txt
前台显示都很正常。感谢感谢。
34. 炜煜 | 2009:03:11 - 13:53:49 | #
囧。functions.php中,我把custom_pings这个函数拷贝到custom_comments的函数下。保存就不会出现页面空白的问题了。我这个PHP白痴,不知道为啥会这样。总之还是谢谢水煮鱼的教程,您的博客是在太好了。
35. cyang | 2009:06:10 - 21:16:24 | #
这个我看的晕晕的