前面我讲了如何让你的 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')); ?>
这样就大功告成。

cyang | 2009-06-10 21:16:24 | #
这个我看的晕晕的
支持 反对
猪八戒 | 2010-05-16 07:53:08 | #
一看这些代码就头晕
支持 反对