前面我讲了如何让你的 WordPress 主题实现 Thread Comments 功能,但是并没有实现把留言和 Trackbacks 分开,并且也没有把它们进行样式化,那么今天我就讲讲如何把留言和 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 来实现显示的都是留言。
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. 通过上面三个步骤,已经把留言和 Trackbacks 区分开了,但是几个问题依然存在, Trackbacks 也可以回复,留言列表不能适应原有的主题的样式。这个时候我们可以使用 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')); ?>
这样就大功告成。

终于可以复制了,哈哈,这篇文章就有用多了。
…
慢了一步
还是搞不定。。。貌似有点不明白。。。额
难道我还有文章那个没用的?
测试留言回复email通知。
在测试下!
这次测试应该会成功了吧?
Good,留言回复 Email 通知成功。
你动作好快
沙发先
扯平…….扯平,我最喜欢这种结果了.
测试下
这个一定要顶。。
我的留言哪里去了?
很好很强大。我正需要。
谢谢 。
先收藏,等换2.7后再仔细看:)
先加个星号,等着2.7正式版
先本地试下…2.7…哇塞…吼吼
老大,你用的啥编辑器哟……
高亮颜色不错哟……
Coolcode 并且帮我看看能否收到回复的 email。
这篇文章的效果就是现在这样可以回复留言吗!
期待2.7的发布啊
你的存档页面是怎么实现的呢?
呵呵,有点意思~~
水煮鱼已经用上2.7了呀, 恭喜恭喜, 不过这样修改主题好像并不是最好吧? 应该是自定义一个类好像不错.
恩,不会类,太累了。
期待正式版的发布在升级啦!
我的comments.php 都没有wp_list_comments
函数?
我的出现这个错误:
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) {
6. 自定义 Comments 列表函数
最后应该不用。。我刚才发现产生结构不对。。查了一下官方发现的
最近在試如何讓他人回覆的圖像顯示
找到這幾篇文章
只是要修改的地方可真多
感谢好文章,我按照您的方法修改了之后,确实如愿了。不过我给主题下的functions.php添加了2个函数,结果就造成只要一有数据提交,就页面空白的问题。不管前台后台都一样。哪位大侠帮忙看看我的functions.php文件是不是哪里写错了?functions.php地址:
http://9npc.com/wp-content/themes/PureCSS/functions.php.txt
前台显示都很正常。感谢感谢。
囧。functions.php中,我把custom_pings这个函数拷贝到custom_comments的函数下。保存就不会出现页面空白的问题了。我这个PHP白痴,不知道为啥会这样。总之还是谢谢水煮鱼的教程,您的博客是在太好了。
这个我看的晕晕的
一看这些代码就头晕
恩 这个问题已经困扰了我很久了啊 根本不太明白那是什么东西