在WordPress上将评论与Trackback分开显示

本文转载自WordPress啦!

早在WordPress 2.7版本发布时,WordPress团队就开发了一种全新的评论样式, 其中包括嵌套回复,评论的处理发生了戏剧性变化。不幸的是,这一变化却打破了最受欢迎的评论方式:trackback与评论分开

从那以后,有人就开始想办法在WordPress 2.7及以上版本的博客中实现分离trackback和评论。我在Sivel.net 中找到了目前为止最好的方法,大家可以通过这里查看。按照那里提示的步骤操作就可以了。

注意: 上面的指南只适用于WordPress 2.7 或以上版本。如果是WordPress 2.6或更旧的版本用户,请查看这里。

成功将trackbacks和评论分开后,还可以通过几个小步骤来完善显示效果。首先就是将你的trackbacks/pingback只显示标题而不显示摘要和所有的其他东西,看起来更简洁了。在comments.php 找到下面的代码:

<ol>
<?php wp_list_comments(‘type=pings’); ?>

将其替换成:

<ol>
<?php wp_list_comments(‘type=pings&callback=list_pings’); ?>

然后把以下代码添加到functions.php中:

<?php
function list_pings($comment, $args, $depth) {
$GLOBALS['comment'] = $comment;
?>
<li id=”comment-<?php comment_ID(); ?>”><?php comment_author_link(); ?>
<?php } ?>

这样trackbacks/pingbacks 区域就比较简洁了,你也可以通过插件显示tweetbacks。

另外,你可能还想修改评论数量,让它显示实际的评论数量,也就是筛除默认评论中包含的trackbacks/pingbacks评论数。只要在functions.php中添加下面的代码即可:

<?php
add_filter(‘get_comments_number’, ‘comment_count’, 0);
function comment_count( $count ) {
if ( ! is_admin() ) {
global $id;
$comments_by_type = &separate_comments(get_comments(‘status=approve&post_id=’ . $id));
return count($comments_by_type['comment']);
} else {
return $count;
}
}
?>

附原文:

Separating Trackbacks from Comments in WordPress 2.7+

Back when WordPress 2.7 was released, the WordPress team introduced a completely revamped comment form that included integration of threaded comments into the core software, introducing some dramatic changes with how comments are handled.   Unfortunately, this change broke one of the most popular comment hacks, separating trackbacks from comments.

Since then, several people have stepped up and shared some great hacks for separating trackbacks from comment in WordPress 2.7 or newer blogs .  So far the best guide I’ve found came from Sivel.net, which can be viewed here.  Click over and follow those steps get everything separated.

Note: The above guide is only for people using WordPress 2.7 or newer installations.  For people using WordPress 2.6 or earlier, you’ll want to use this tutorial.

Once you’ve got the comments successfully separated from the trackbacks, there are a couple additional tweaks you may want to do to clean up how things look (it really depends on preference I suppose).   The first is to clean up your trackbacks/pingbacks by only displaying the title instead of an excerpt and everything else.   In order to do this, you’ll need to find the following code in your comments.php file:

<ol>
<?php wp_list_comments('type=pings'); ?>

Now replace that code with the following:

<ol>
<?php wp_list_comments('type=pings&callback=list_pings'); ?>

Lastly, you’ll need to add the following code to your functions.php file (which can be created if you don’t already have one):

<?php
function list_pings($comment, $args, $depth) {
$GLOBALS['comment'] = $comment;
?>
<li id="comment-<?php comment_ID(); ?>"><?php comment_author_link(); ?>
<?php } ?>

That should clean up the trackbacks/pingbacks section and you can also apply the same changes if you use a plugin to display tweetbacks.

The other thing you may want to do is fix the comment count to only show actual comments, filtering out the trackbacks/pingbacks which are included in your comment count by default.   Simply add the following code to your functions.php file (which again can be created if you don’t already have one):

<?php
add_filter('get_comments_number', 'comment_count', 0);
function comment_count( $count ) {
if ( ! is_admin() ) {
global $id;
$comments_by_type = &separate_comments(get_comments('status=approve&post_id=' . $id));
return count($comments_by_type['comment']);
} else {
return $count;
}
}
?>

Tags:

Leave a Reply