WordPress 2.7 Separate 留言和样式化留言
November 3rd, 2008 Denis
上次我讲了如何让你的主题适合 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')); ?>
本文主要参考 Separating Pings from Comments in WordPress 2.7。





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