Separate Trackbacks from Comments on Your WordPress Blog

by Daniel in 41 Comments — Updated Reading Time: 2 minutes

Background Image

I wonder why WordPress theme designers don’t implement this feature more often. Separating trackbacks from comments is a very simple tweak that will greatly improve the user experience for the people that comment or read comments on your blog. If you keep them together (as I used to until recently…) the readers will see a bunch of links and snippets of code mixed with the real comments, making it difficult to follow the conversation.

So how do you do it? You will just need to open the comments.php file and edit some lines. Michael from Pro Blog Design has a step by step tutorial for this purpose, and he allowed me to adapt it here.

First of all locate the following line inside your comments.php file (which is located under “Presentation” and then “Theme Editor” on your WordPress control panel).

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

Right after that line, paste the following code:

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

These lines will identify the type of the comment in question, and it will only publish real comments, while filtering trackbacks and pingbacks. Now you will need to close the PHP function, so again look for the following line:

<?php endforeach; /* end for each comment */ ?>
Before that line paste the following code:

<?php } /* End of is_comment statement */ ?>

That is it; you are now filtering out all the trackbacks and pingbacks. Some people consider it better to remove trackbacks altogether since they are often target of spammers. I don’t agree with this solution. Trackbacks are an essential part of the blog world, just improve your spam blocking tools and you should be fine.

The question then becomes: should I place the trackbacks before or after the comments? Both methods can work, depending on what you are trying to accomplish. Placing the trackbacks right after the post (and before the real comments) might encourage people to link to your posts since their trackback links will be displayed prominently. Placing the comments first, on the other hand, will encourage readers to leave more comments and to follow the conversation more closely.

The code to add a “Trackbacks” section is the following:
<h3>Trackbacks</h3>
<ol>
<?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; ?>
</ol>

Notice that you can customize the title (“Trackbacks,” “Backlinks,” “Blog Reactions” and so on), the header tag (H1, H2, H3 and so on and the list type (ordered or unordered).

Now, if you want to insert the “Trackbacks” section before the real comments you will need to paste that code after the comments header, which looks like this:

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

You might also want to add another header before the real comments to call them out.

Finally, if you want to place the “Trackbacks” section after the real comments just paste the code before this line:

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

Share this article

41 thoughts on “Separate Trackbacks from Comments on Your WordPress Blog”

  1. salenco: unfortunately it seems this method is no longer a solution as worpdress has changed the way it works in 2.8.4. i wish this article would have the date it was posted as then it would be obvious if something was a little old or not

    Reply
  2. Nice tutorial, but I cant find in comments.php. Im using WordPress 2.8.4 with the last Cutline template with one right column.

    Thanks for reading.

    Reply
  3. Thank you so much! this has helped me solve a long standing problem on a theme I’m developing with on my testing server

    Reply
  4. VERY nice!

    I was gonna implement this on my theme but when I looked it was already implemented! 😀

    I think nobody trackbacked me at all :/

    Reply
  5. STUMBLED!

    I definitely agree with this. Nothing worse than trying to read comments and having to wade through trackbacks.

    Submitted this post to:

    Reply
  6. In one of my blogs I have implanted east code and I have had it to clear because it gave a problem me in post that has 103 commentaries, was not finished loading the page. 🙁

    Reply
  7. Good to see the code and thanks for providing that. I look forward to seeing Davids for/ each code to make this even more useful, thanks

    Reply
  8. @ David Zemens (comment #14)

    Great tip…I implemented it per your code. Just make sure when you add this code in the loop

    it’s above this code

    I had it the other way around and if you don’t have any comments on the post but some trackbacks, the trackbacks won’t show.

    Great article overall..thanks for the tips!

    ~David

    Reply
  9. Ouch! I got my theme designed by Micheal, but it doesn’t separate comments from trackback. Thanks for the reminder, I’ll do it right away.

    Reply
  10. There’s a terrific plugin for this purpose that will exactly do all this for you, but then automatically. Advantages: less work, no extra work when you upgrade WordPress, and numerous other interesting options regarding how to display your comments.

    Reply
  11. Thanks! That wasn’t painful at all. But, to have the trackbacks appear before the comment headings, I had to insert the `Trackbacks section BEFORE the comments header – instructions say after. Or did I misinterpret something?

    Reply
  12. Yes, I learnt from Michael when he posted his tutorial. My blog looks much better now by having the trackbacks nicely separated from the comments. Definitely neater that way.

    Reply
  13. Good to see the code and thanks for providing that. I look forward to seeing Davids for/ each code to make this even more useful, thanks

    Reply
  14. I have been playing with my theme over the past few weeks.

    I found a tutorial about separating the comments and trackbacks. It involves a separate function (split_comments) that I have in my functions.php file. My theme is kind of warped, so I did some major overhauling of it and hopefully made it a little easier to work with.

    I have included Gravatars in the comments, separated the comments and trackbacks, and after reading some of these comments, I added a simple if statement to keep from displaying the Comments header when there are only trackbacks. I think the split_comments function is what allows me to easily run the if statement.

    I just used that code, modified it a bit, and then threw in the following right before the Comments header.

    if ( count( $bm_comments ) > 0 ) {

    }

    Reply
  15. In order to limit the Trackbacks header to displaying only when their actually are one or more trackbacks, I modified your code to assign a variable in your original For/Each loop:

    <?php if($comment_type != ‘comment’) { $is_trackback = “is_trackback”; } ?>

    This code checks to see if ANY of the comments in the loop are
    trackbacks, and assigns the variable as shown.

    Then, in the code that displays the actual trackbacks (which in my case
    is below the comments) this is my code:
    <?php if ($is_trackback == “is_trackback”) { ?>
    <h4>Trackbacks to Other Blogs</h4>
    <ul id=”trackbacks”>
    <?php foreach ($comments as $comment) : ?>
    <?php $comment_type = get_comment_type(); ?>
    <?php if($comment_type != ‘comment’) { ?>
    <li>

    Reply
  16. Thanks for the credit link Daniel. Separating out the trackbacks is one of the easiest ways to improve commenting on a blog. They’re a nusiance otherwise! 🙂

    Reply
  17. I wrote about this some time ago and use a slightly different method.

    I am also making a theme that makes use of the same code. I’ve been thinking about making a plugin that does this for you but am not sure how best to work it since people style and lay out their comments in different ways.

    Reply
  18. Great tip. I think it needs a bit of extra work in order to display the “Trackbacks” header only if there are any trackbacks. I’ll see if I can hack it.

    Reply
  19. Terrific post! I was literally wondering how to accomplish this when I stumbled on your post. Great advice. I implemented it on my blog within five mintues and it works great!

    Thank you.

    Reply

Leave a Comment