Want to know when there are new posts?

SUBSCRIBE VIA EMAIL

SUBSCRIBE VIA RSS

Aug 7th

Separate Your Trackbacks From Your Comments

Topics: All Things Wordpress Code

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.

What’s the solution?

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.

Step One - Counting Comments and Trackbacks

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.

Step Two - Showing the Comment Count

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 &#8220;<?php the_title(); ?>&#8221;

This will spit out the comment count in this format:
1 Response for ‘Post Title’
5 Responses for ‘Post Title’

Step Three - Displaying The Comments

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.

Step Four - Displaying Trackbacks and 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.

Tags: , , , ,



RSS Feeds

MiniSitePacks.com

95% Of Your Minisite Design Done For You--INSTANTLY?

This is a complete solution! We're Talking Full Blown Graphics--From Top To Bottom--At A FRACTION Of What You Would Pay A Designer! Find out more.

Related Posts

What Do You Think?

Enter your name as YourName@YourKeywords and YourKeywords will be used as the anchor link to your website. Psst this gives you Page Rank advantage.

  1. (required)
  2. (required)
  3. (required)

Comment Preview:

Just in case you wanna see.

You can also subscribe to comments via RSS Comments RSS