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

Leave a Comment