...

How to Separate Trackbacks from Comments in WordPress

132

How to Separate Trackbacks from Comments is no new hack, but when WordPress released version 2.7, they introduced a new renovated comment system which included threaded comments, ability to paginate and much more. But along with this change, they also changed a lot of core file parameters. In this article we will show you how you can separate trackbacks from comments in WordPress. This hack will only work for version 2.7+ and if you are not using it, then you should start now because of the recent MySQL attack on older versions.

We found this tutorial on one of the WordPress developer’s website called Sivel.net

Here is an example of the new loop that we will be referring to in the tutorial:

      <?php if ( have_comments() ) : ?>      <h3 id="comments"><?php comments_number('No Responses', 'One Response', '% Responses' );?> to %u201C<?php the_title(); ?>%u201D</h3>      <ol class="commentlist">      <?php wp_list_comments(); ?>      </ol>      <div class="navigation">      <div class="alignleft"><?php previous_comments_link() ?></div>      <div class="alignright"><?php next_comments_link() ?></div>      </div>        <?php else : // this is displayed if there are no comments so far ?>        <?php if ('open' == $post->comment_status) : ?>      <!– If comments are open, but there are no comments. –>        <?php else : // comments are closed ?>      <!– If comments are closed. –>      <p class="nocomments">Comments are closed.        <?php endif; ?>      <?php endif; ?>

Find this code in your comments.php:

  <?php if ( have_comments() ) : ?>

Directly below this code add the following code:

  <?php if ( ! empty($comments_by_type['comment']) ) : ?>  [/php    Once you have added the above code then find this code:      <?php wp_list_comments(); ?>

Replace the above code with the following code:

  <?php wp_list_comments('type=comment'); ?>

Now as you see in our example loop there is a code for ordered list that looks like

  </ol>

Directly below this code add:

  <?php endif; ?>

Now by adding the endif tag, if you do not have any comments, the ordered list will not be displayed. Now lets move on to adding the pings to the comments.

Add the following code below or however you want to display it. It will display the pings.

      <?php if ( ! empty($comments_by_type['pings']) ) : ?>      <h3 id="pings">Trackbacks/Pingbacks</h3>      <ol class="commentlist">      <?php wp_list_comments('type=pings'); ?>      </ol>        <?php endif; ?>

Now when you have this it will display the trackbacks but it will show them just like comments. Now you may want to display them as a list because other wise you are just wasting space. So here is how you can do that.

Simply open functions.php which is in your themes folder and add the following function in there:

  <?php  function list_pings($comment, $args, $depth) {  $GLOBALS['comment'] = $comment;  ?>  <li id="comment-<?php comment_ID(); ?>"><?php comment_author_link(); ?>  <?php } ?>

This function will let you display the pings as a list instead of showing like a comment. But you have to do one more thing.

Open your comments.php and find this code:

  <ol>  <?php wp_list_comments('type=pings'); ?>

Replace it with:

  <ol>  <?php wp_list_comments('type=pings&callback=list_pings'); ?>

Now the final copy of the example loop would look this:

      <?php if ( have_comments() ) : ?>      <?php if ( ! empty($comments_by_type['comment']) ) : ?>      <h3 id="comments"><?php comments_number('No Responses', 'One Response', '% Responses' );?> to %u201C<?php the_title(); ?>%u201D</h3>      <ol class="commentlist">      <?php wp_list_comments('type=comment'); ?>      </ol>        <?php endif; ?>        <?php if ( ! empty($comments_by_type['pings']) ) : ?>      <h3 id="pings">Trackbacks/Pingbacks</h3>      <ol class="pinglist">      <?php wp_list_comments('type=pings&callback=list_pings'); ?>      </ol>        <?php endif; ?>      <div class="navigation">      <div class="alignleft"><?php previous_comments_link() ?></div>      <div class="alignright"><?php next_comments_link() ?></div>      </div>        <?php else : // this is displayed if there are no comments so far ?>        <?php if ('open' == $post->comment_status) : ?>      <!– If comments are open, but there are no comments. –>        <?php else : // comments are closed ?>      <!– If comments are closed. –>      <p class="nocomments">Comments are closed.        <?php endif; ?>      <?php endif; ?>

Now you are done. There is a bonus hack that you can use. Since you are displaying them separately, it would be good if you display the accurate comment count by excluding trackbacks and pings. Use this tutorial that we wrote to display the most accurate comment count in WordPress.

Seraphinite AcceleratorOptimized by Seraphinite Accelerator
Turns on site high speed to be attractive for people and search engines.