Separating Trackbacks and Comments in WordPress 2.7+
August 30th, 2009
How-To, Tutorials, WordPress Tutorials
Interested in separating your trackbacks and comments on your WordPress blog? This blog tutorial should have you covered, whether you use a modern WordPress installation, or an older (pre 2.7) version of WordPress. Below you’ll find we’ve put together a tutorial for both types of comment loops.
For WordPress 2.7+ Installations
Locate the following code in your index.php or single.php file (whichever file your theme uses to show single post pages):
<?php comments_template(); ?>
Replace that code with the following:
<?php comments_template('', true); ?>
That is it for that file. Now go to your comments.php file and locate the following code:
<?php if ( have_comments() ) : ?>
Immediately below that code, insert the following code:
<?php if ( ! empty($comments_by_type['comment']) ) : ?>
Okay, once that is done, you’ll next need to scroll down a little further (still in the comments.php file) and locate the following code:
<?php wp_list_comments(); ?>
You’ll want to replace that code with the below code:
<?php wp_list_comments('type=comment'); ?>
Immediately below this function you should see the following code:
</ol>
Directly below that code, place the following code:
<?php endif; ?>
That should take care of the comment loop. The last step is to insert the code which will display the trackbacks/pingbacks. This can be placed anywhere below the code we just hacked above:
<?php if ( ! empty($comments_by_type['pings']) ) : ?>
<h3 id="pings">Trackbacks/Pingbacks</h3>
<ol>
<?php wp_list_comments('type=pings'); ?>
</ol>
<?php endif; ?>
Some people prefer to display their trackbacks and pingbacks below the comment form or somewhere else out of the way, so this is really up to you! Just place the above code where you want to show them. If you don’t like them showing at all on your blog, just don’t place the above code at all.
Once that is done, it is time to give it a try. Check it out on your blog and let us know how everything turns out!
For Pre-WordPress 2.7 Installations:
Prior to WordPress 2.7, the comment form wasn’t nearly as functional as it is with the newer versions and also included a lot more code, but it is still possible to separate the trackbacks and comments using the old comment loop. If you’d like to separate your trackbacks from your comments on a WordPress 2.6 or earlier installation, here is the steps you’ll need to take.
First, access your comments.php file and grab the following code:
<?php foreach ($comments as $comment) : ?>
Immediately after the above code, you’ll want to place the following code:
<?php $comment_type = get_comment_type(); ?>
<?php if($comment_type == 'comment') { ?>
Next, scroll down a little bit and locate the following code:
<?php endforeach; /* end for each comment */ ?>
Immediately before the above code, you’ll want to place this code:
<?php } /* End of is_comment statement */ ?>
This will filter out all of the trackbacks and pingbacks from your main comments loop. Now we need to create a second comments loop to display the trackbacks and pingbacks. Almost immediately below the code from step 2 you should find this code:
<?php else : // this is displayed if there are no comments so far ?>
Immediately before the above code, you’ll want to place this code:
<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>
You can adjust this code to display how you want to, including using a different header if you have a specific look for your header 3.
If you’ve tried this tutorial, let us know how it worked for you in the comments!

