Oct 7
icon1by Gilmar Jimeno | Wordpress | Leave a comment
Social Networking Icons

Trackbacks are one way of telling the web authors that another site links back to one of their articles, posts and the like. This helps the authors know who is linking to their articles. WordPress happens to be one of the blogging platforms that support automatic pingbacks or trackbacks.

So, why do you have to separate trackbacks from the comments then? One reason is to make things look more tidy. Isn't it nicer to see all the apples in one row and the oranges in the other than see them mixed altogether on a fruit stand? Besides the comments are the thoughts shared by real people, and you, the one reading them, just don't want to be interrupted with those machine-generated linkbacks in between.

Here's now a simple way of segregating comments and trackbacks. In this hack we will be editing comments.php. And before anything else, you should always create a backup copy of the file or simply just the code.

Step 1

Now open comments.php, and look for the following code:

<?php foreach ($comments as $comment) : ?>
After that, paste this:
<?php $comment_type = get_comment_type(); ?> 
<?php if($comment_type == 'comment') { ?>

EXPLANATION:

In the code we added, line 1 means that we will get the type (comment or trackback) of all the comments it parses for the specific post. Then line 2 will filter and show only the ones with the type comment. Thus your comments will be posted as normal but without the trackbacks or pingbacks along.

Step 2

Search for:
<?php endforeach; /* end for each comment */ ?>
Then before the above code, add this:
<?php } else { $trackback = true; } /* End of is_comment statement */ ?>

EXPLANATION:

The appended line of code means that if it has filtered comments with type trackback, it shall set the variable $trackback to true, but nothing of those trackbacks will appear still. Don't worry we really didn't discard those precious trackbacks completely because we've got another way of handling them which we will do in the succeeding step.

Step 3

Look for this code:
<?php else : // this is displayed if there are no comments so far ?>
Before it, paste the code below:
<?php if ($trackback == true) { ?>
  <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>
<?php } ?>

EXPLANATION:

In line 1, if there are trackbacks found then it shall execute the inside code. "Trackbacks" in line 2 will be the title header for the trackbacks or pingbacks section. As you can see, "Trackbacks" will only appear if there are any trackbacks or pingbacks available. Then the rest of the code is the default way of rendering trackbacks which is just similar to comments (like having its author, author's link, etc).

You're done! Try refreshing your blog and see if it works. This hack would only be useful if you really are using trackbacks or pingbacks with your posts. So have fun.

   
 
Want an avatar?

Go to gravatar.com and upload your preferred avatar.