有时候,我们可能在某个地方显示评论/Trackback/Pingback的数目。如果说,这些都没有区分开来。那么我们可以用简单的方法来完成,如下:
1. 显示所有评论(包括Trackback/Pingback)的数目
- 打来comments.php找到下面这一行:
<?php foreach ($comments as $comment) : ?>
将其修改成:
这时,你就可以在这个评论的循环中加往下面的代码来显示评论的数目:<?php $i = 0; ?> <?php foreach ($comments as $comment) : ?> <?php $i++; ?>
<?php echo $i; ?>
2. 分别显示评论/Trackback/Pingback的数目
不过。当需要区分评论/Trackback/Pingback的数目时,第1点所提到的方法就不再适应。因此,我们添加一个filter来区分他们,以达到我们区分显示的目的。方法如下:
- 在functions.php中加入如下代码:
/** * 显示Trackback, pingback, comment, pings的数目,使用: * fb_comment_type_count('ping'); * fb_comment_type_count('comment'); */ if ( !function_exists('fb_comment_type_count') ) { function fb_get_comment_type_count($type='all', $post_id = 0) { global $cjd_comment_count_cache, $id, $post; if ( !$post_id ) $post_id = $post->ID; if ( !$post_id ) return; if ( !isset($cjd_comment_count_cache[$post_id]) ) { $p = get_post($post_id); $p = array($p); update_comment_type_cache($p); } if ( $type == 'pingback' || $type == 'trackback' || $type == 'comment' ) return $cjd_comment_count_cache[$post_id][$type]; elseif ( $type == 'pings' ) return $cjd_comment_count_cache[$post_id]['pingback'] + $cjd_comment_count_cache[$post_id]['trackback']; else return array_sum((array) $cjd_comment_count_cache[$post_id]); } // comment, trackback, pingback, pings function fb_comment_type_count($type = 'all', $post_id = 0) { echo fb_get_comment_type_count($type, $post_id); } }当functions.php中的function成功启用的话,显示这三者的数目的方法如下:
if ( function_exists('wp_list_comments') ) { // WP 2.7 评论循环 if ( have_comments() ) { ?> <?php if ( empty($comments_by_type['comment']) ) { ?> <h2 id="comments"><?php comments_number('Keine Kommentare', 'Ein Kommentar', '% Kommentare' );?></h2> <ol> <?php wp_list_comments('type=comment&callback=fb_theme_comment'); ?> </ol> <?php } ?> <?php if ( function_exists( 'fb_comment_type_count' ) ) { if ( empty($comments_by_type['pingback']) ) { ?> <h2 id="pingback"><?php fb_comment_type_count('pingback'); ?> Trackbacks/Pingbacks</h2> <ol> <?php wp_list_comments('type=pingback'); ?> </ol> <?php } ?> <?php if ( empty($comments_by_type['trackback']) ) { ?> <h2 id="trackback"><?php fb_comment_type_count('trackback'); ?> Trackbacks/Pingbacks</h2> <ol> <?php wp_list_comments('type=trackback'); ?> </ol> <?php } } ?> <?php } else { // 这里放入没有评论时显示的内容 } ?>

加上自定义的一些样式就成为一个可用的留言列表了。。