Separate trackbacks from comments [updated]

This article describes a way of separating trackbacks from comments in your Wordpress theme when you are using Wordpress 2.7+. I have noticed that a lot of tutorials on the internet that explain how this is done are based on older versions of Wordpress. These tutorials don’t work anymore because Wordpress uses a different way of displaying comments now. This new way makes it easier to separate trackbacks from comments but the outdated tutorials can be very confusing for inexperienced users who want to separate trackbacks from comments in their own Wordpress theme and rely on these tutorials.

If you don’t know the difference between comments and trackbacks but you have become interested, please read this first before you read on.

Comments vs. trackbacks

At the time of writing Wordpress by default (in the default theme) does not separate trackbacks from regular comments. When kept together the trackbacks are shown as links and snippets of text in the same list as the actual comments posted by users. Separating the two can greatly improve the user experiences for the people who comment or read the comments on your blog and it looks much cleaner in your theme as well. You see more and more theme designers are implementing this feature in their themes but I think it should be done by default. If you run Wordpress 2.7+ and you want to separate the trackbacks from the comments in your own them then follow the instructions below.

Separate trackbacks from comments

First we need to edit single.php located in your active theme directory. Editing can be done through the theme editor from the admin panel (Appearance > Editor) if the file permission settings allow you to do this or by uploading an edited version of the file through FTP. Either way, always make a backup before you start editing.

Find the following:

<?php comments_template(); ?>

Replace this with:

<?php comments_template('', true); ?>

What this does is creating a global parameter which we will use later on to hide the comments or trackback lists if there are none to display. Next you will need to edit the comment.php.

Locate the following code in comment.php:

<?php if ( have_comments() ) : ?>

Directly below this add the following:

<?php if ( ! empty($comments_by_type['comment']) ) : ?>

Next locate the following code:

<ol class="commentlist">
	<?php wp_list_comments(); ?>
</ol>

Replace this code with:

	<ol class="commentlist">
		<?php wp_list_comments('type=comment'); ?>
	</ol>
<?php endif; ?>

<?php if ( ! empty($comments_by_type['pings']) ) : ?>
	<h3>Trackbacks / pingbacks</h3>
	<ol class="trackbacklist">
		<?php wp_list_comments('type=pings'); ?>
	</ol>
<?php endif; ?>

This change will create separate ordered lists for comments and trackbacks. The first list for comments and the second for trackbacks and pingbacks. The if statements ensures that a list is not shown if there are no comments or trackback. If you also want to separate trackbacks and pingbacks you should replace the word pings with either trackback or pingback.

Update the comment count

If you also want to exclude the trackbacks in the comment count then add the following code at the bottom of your functions.php. If you do not have this file in your theme directory then create it with your text editor.

<?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;
        }
}
?>

That’s it, you are done. I would love to hear from you if this has been useful to you.

If you think this is useful, help promote this article

3 Responses

  1. Quote
    April 2nd, 2010 at 15:03 [ permalink ]

    Thank you. All of the code worked straight away, also written well.

  2. Quote
    July 21st, 2010 at 15:52 [ permalink ]

    This is a great tutorial. After many failed attempts with other tutorials, this one was exactly what I was looking for. I wanted to separate trackbacks from comments along with the numbering and this worked beautifully. The code for functions.php was perfect. Thanks so much.

1 trackback(s) / pingback(s)

  1. [...] couple of days ago I wrote a simple tutorial on how you can separate trackbacks from comments. This tutorial didnt do much [...]

Leave a comment

Copyright © Blogging About. Some rights reserved.