IN Code
posted Thursday, August 7th, 2008
It is amazing how many blogs do not do this. They have the messiest comments section with a zillion comments, trackbacks and pingbacks all jumbled up. And it is almost impossible to make some sense of out of the entire mess. Want to follow the conversation? Try evading the trackbacks and pingbacks along the way.
Separate the two, of course.
But How?
That’s a good question. You will find plugins that do this but if you can manage something in the theme design, it is best to do so rather than install plugin upon plugin. Plugins are best used where you cannot achieve your objective without touching the core WordPress files.
In this case all you need to work with is your theme’s comments.php file.
Since the normal comment count includes both the comments and trackbacks, we first need to get a separate count for each.
Find this line in comments.php
<!-- You can start editing here. -->
Add the following code under it
<?php
$numTrackBacks = 0;
$numComments = 0;
foreach ($comments as $comment) {
$comment_type = get_comment_type();
if ($comment_type != 'comment') { $numTrackBacks++; }
else { $numComments++; }
}
?>
Basic gist:
First we set $numTrackBacks and $numComments as 0. We then check the database and loop thru the comments and trackbacks. If the record retrieved is not a comment then a number is added to the trackback count otherwise the comment count is increased.
Next we need to show the comment count before we display the comments.
Find this line
<?php if ($comments) : ?>
and add this code after it:
<?php echo $numComments;?>
<? if ($numComments = 1) :?>
Response
<? else : ?>
Responses
<? endif; ?>
to “<?php the_title(); ?>”
This will spit out the comment count in this format:
1 Response for ‘Post Title’
5 Responses for ‘Post Title’
Next we have to display comments. Look for this line:
<?php foreach ($comments as $comment) : ?>
And add these two lines after it:
<?php $comment_type = get_comment_type(); ?>
<?php if($comment_type == 'comment') { ?>
What we are doing here is checking to see if the record retrieved is a comment.
If so then we display it.
Since we have opened a loop, we also need to close it.
Find the line
<?php endforeach; /* end for each comment */ ?>
and replace it with:
<?php } /* End of is_comment statement */ ?>
<?php endforeach; /* end for each comment */ ?>
We have successfully displayed our comments without any trackbacks or pingbacks.
Time to display our trackbacks and pingbacks.
Add this code right after you finish displaying the comments (in most themes it would be after the /ol tag):
<? if ($numTrackBacks != 0) :?>
<br />
<h3>Trackbacks and Pings
(<? echo $numTrackBacks; ?>)</h3>
<? endif; ?>
<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 change the styling and format to suit your site. However, this basic code will show your trackbacks and pingbacks separated from your comments.
–
Now there is no excuse to not clean up your blog and organize your comments.