(+84) 463.28.7979

How to Separate Pingbacks and Trackbacks from Comments


According to Wikipedia “A trackback is one of three types of linkbacks, methods for Web authors to request notification when somebody links to one of their documents. This enables authors to keep track of who is linking, and so referring, to their articles”.

separate_coment_trackback2_128

Most bloggers don’t show pingbacks and tracbacks on their blog but if you want to show them then I recommend separating pingbacks and trackbacks from the comments. The main reason I like to separate them is because comments are sometimes an ongoing conversation and trackbacks in the middle kinda breaks the flow of the conversation.

Here is how you can separate the pingbacks and trackbacks from the comments.

search for the following line in your comments.php template file

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

Put the following two lines just after the above line.

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

Now look for the end of the ‘for’ loop so you can close the if statement that you just introduced. To do that search for the following line

endforeach;

Just above that line insert the following code to end the if statement


<?php } ?>

This will filter out the trackbacks and only show comments in this section. If you don’t want to show the pingbacks and trackbacks then you are done. If you do want to show the trackbacks just after the comments section then insert the following piece of code just after the end of for loop section (search for endforeach;)


<?php
/* Loop through comments to count the total pingbacks */
$numPingBacks = 0;
foreach ($comments as $comment) {
if (get_comment_type() != "comment") {
$numPingBacks++;
}
}
?>
<?php if ($numPingBacks != 0) { ?>
<h4>Backlinks/Trackbacks</h4>
<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>
<?php } ?>

To see a demo of how this will look like (comments and trackbacks separated) see my WordPress PayPal Shopping Cart plugin post that has quite a large number of trackbacks and comments. I hope this helps.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>