Separating Comments and Trackbacks

17 Comments

One thing I don’t love about WordPress, is its way of including comments, pingbacks, and trackbacks all in one list, and doesn’t provide an easy option to separate them. Separating these gives your comments area a cleaner, more professional feel. Here’s how to do it:

You’ll need a self-hosted copy of WordPress 2.0 or higher to do this. This functionality was added in WordPress 2.0, so if you are running 1.x, you’ll need to upgrade. You’ll need to edit comments.php (located in your theme folder), so you can either use the Theme Editor (WP-Admin -> Presentation -> Theme Editor), or you can download comments.php (located at /wp-content/themes/yourtheme/comments.php) and edit it in your favorite code editor.

Now we have to start editing the code! First of all, look for the following code:

<?php foreach ($comments as $comment) : ?>

Directly below the above code, paste the following:

<?php $comment_type = get_comment_type(); ?>
<?php if($comment_type == 'comment') { ?>

That code is telling WordPress to get the comment type, and if the comment type is equal to ‘comment’, which is a human comment, display it.

Next, we’ll need to insert some more code. Look further down the comments.php file, until you see:

<?php endforeach; /* end for each comment */ ?>

Place the following code, just above the endforeach code:

<?php } else { $trackback = true; } ?>

Lastly, you’ll need to place the code to display the trackbacks, and also to hide the trackbacks title, if there aren’t any pingbacks or trackbacks yet. Place the code, right before this:

<?php else : // this is displayed if there are no comments so far ?>

Here’s the code to paste before that:

<?php if ($trackback == true) { ?>
<h3>Trackbacks</h3>
<ul id="trackbacks">
<?php foreach ($comments as $comment) : ?>
<?php $comment_type = get_comment_type(); ?>
<?php if($comment_type != 'comment') { ?>
<li><?php comment_author_link() ?></li>
<?php } ?>
<?php endforeach; ?>
</ul>
<?php } ?>

This code is telling WordPress to show all the comments that don’t have the comment type of ‘comment’, to display. This includes pingbacks and trackbacks. Now if you look onto a post page (you might need to reset your cache if you are using WP-Cache or something similar), you’ll notice an area showing comments, and a list of links for the trackbacks and pingbacks.

If you want to place the trackbacks above the real comments instead of below the comments, place the trackbacks code (previous code snippet) directly after this:

<h2 id="comments"><?php comments_number('No Responses', 'One Response', '% Responses' );?> to “<?php the_title(); ?>”

This post was written by Chris Thomson, a young teenage podcaster and blogger.

17 thoughts on “Separating Comments and Trackbacks

  1. Pingback: The Status of my Blog365 ‹ The Blog of Chris Thomson

  2. Hey Kyle,

    I actually saw your post similar to this a while back, but the thing I didn’t like about your version is that “Trackbacks/Pingbacks” is shown all the time, even if they’re no trackbacks. With the version above, it hides the “Trackbacks/Pingbacks” title, if there are none. 🙂

  3. Hey Kyle,

    I actually saw your post similar to this a while back, but the thing I didn’t like about your version is that “Trackbacks/Pingbacks” is shown all the time, even if they’re no trackbacks. With the version above, it hides the “Trackbacks/Pingbacks” title, if there are none. 🙂

    Good thinking!

    I guess I like to have it there all the time as a subtle reminder/recommendation for readers to send a trackback/pingback to display there! A lot of times if you can be the first or second one on a popular post you can generate some traffic for yourself.

  4. Pingback: WordPress Weekly Episode 5 » Jeffro2pt0.com

  5. Pingback: 区分开 Comments 和 Trackbacks

  6. Pingback: 区分Comments和Trackbacks | Broken Arrow

  7. Pingback: 明年今日

  8. Pingback: 区分开 Comments 和 Trackbacks - Mr.bssn 的个人博客

  9. I don’t know how I never really dug deeper into your blog? I read / downloaded the tip sheets yet never realized the wealth of knowledge this site really has. This is yet another tip that I otherwise would have spent some serious time researching how to do the correct way.

  10. Pingback: WordPress Weekly Episode 5 | Jeffro2pt0

  11. Thanks a bunch! I’m a newbie to PHP, but I figured out that i had to change the following characters in your code:

    < change to

    Easily did this in notepad editior.

    Cool!!

Comments are closed.