Separate comments from trackbacks in your WordPress blog

When your blog starts becoming popular, people will visit, read and talk about it and your blog will have comments pingbacks, and trackbacks.

While everybody knows what is a comment, I am going to explain pingbacks and trackbacks.

From WordPress Codex Glossary:

Pingback lets you notify the author of an article if you link to his article (article on a blog, of course). If the links you include in an article you write on a blog lead to a blog which is pingback-enabled, then the author of that blog gets a notification in the form of a pingback that you linked to his article.

Trackback helps you to notify another author that you wrote something related to what he had written on his blog, even if you don't have an explicit link to his article. This improves the chances of the other author sitting up and noticing that you gave him credit for something, or that you improved upon something he wrote, or something similar. With pingback and trackback, blogs are interconnected. Think of them as the equivalents of acknowledgements and references at the end of an academic paper, or a chapter in a textbook.

Unfortunately WP's default theme (and a lot of other themes too) includes comments, pingbacks and trackbacks all together.

It's not the best solution in my opinion because it would be better to have a main list with only comments, followed by the form to leave a comment and later trackbacks and pingbacks.

In order to do it, we only need get_comment_type() function.

This function returns comment, pingback or trackback according to the type of comment.

So it's time to edit comments.php file. You will find this file in your theme directory.

Search for

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

and add below:

PHP:
  1. <?php if (get_comment_type() == "comment") { ?>

to determine whether the current comment is a real comment or not.

Then search for

PHP:
  1. <?php endforeach; /* end for each comment */ ?>

and add before:

PHP:
  1. <?php } else { $i_have_trackbacks = true; } ?>

At this time, you will have your comments list containing only comments, and a boolean variable called $i_have_trackbacks set to true if the script found at least a comment that's a pingback or a trackback.

Now, after the

HTML:

tag that should close the form used to leave a comment, I repeated the main comment script this way:

PHP:
  1. <?php if($i_have_trackbacks) { ?>
  2.  
  3. <h3>Trackbacks</h3>
  4.  
  5.      <ol class="commentlist">
  6.  
  7.     <?php foreach ($comments as $comment) : ?>
  8.    
  9.     <?php if (get_comment_type() != "comment") { ?>
  10.  
  11.         <li class="<?php echo $oddcomment; ?>" id="comment-<?php comment_ID() ?>">
  12.             <?php comment_author_link() ?> on
  13.             <?php if ($comment->comment_approved == '0') : ?>
  14.             <em>Your comment is awaiting moderation.</em>
  15.             <?php endif; ?>
  16.             <?php comment_date('F jS, Y') ?> <?php comment_time() ?> <?php edit_comment_link('Edit','',''); ?>
  17.  
  18.             <div class="commenttext">
  19.             <?php comment_text() ?>
  20.             </div>
  21.  
  22.         </li>
  23.  
  24.     <?php /* Changes every other comment to a different class */
  25.         if ('alt' == $oddcomment) $oddcomment = '';
  26.         else $oddcomment = 'alt';
  27.     ?>
  28.    
  29.     <?php } ?>
  30.  
  31.     <?php endforeach; /* end for each trackback */ ?>
  32.  
  33.      </ol>
  34.  
  35. <?php } ?>

As you can see from line 1, the scripts is executed only if I found at least a comment that's a pingback or a trackback, while at line 9 I am looking for comments that aren't real comments.

My completed comments.php now looks this way

PHP:
  1. <?php // Do not delete these lines
  2.     if ('comments.php' == basename($_SERVER['SCRIPT_FILENAME']))
  3.         die ('Please do not load this page directly. Thanks!');
  4.  
  5.     if (!empty($post->post_password)) { // if there's a password
  6.         if ($_COOKIE['wp-postpass_' . COOKIEHASH] != $post->post_password) {  // and it doesn't match the cookie
  7.             ?>
  8.  
  9.             <p class="nocomments">This post is password protected. Enter the password to view comments.<p>
  10.  
  11.             <?php
  12.             return;
  13.         }
  14.     }
  15.  
  16.     /* This variable is for alternating comment background */
  17.     $oddcomment = 'alt';
  18. ?>
  19.  
  20. <!-- You can start editing here. -->
  21.  
  22. <div id="commentblock">
  23. <?php if ($comments) : ?>
  24.     <p id="comments"><?php comments_number('No Responses', 'One Response', '% Responses' );?> to &#8220;<?php the_title(); ?>&#8221;</p>
  25.  
  26.     <ol class="commentlist">
  27.  
  28.     <?php foreach ($comments as $comment) : ?>
  29.    
  30.     <?php if (get_comment_type() == "comment") { ?>
  31.  
  32.         <li class="<?php echo $oddcomment; ?>" id="comment-<?php comment_ID() ?>">
  33.             <?php comment_author_link() ?> on
  34.             <?php if ($comment->comment_approved == '0') : ?>
  35.             <em>Your comment is awaiting moderation.</em>
  36.             <?php endif; ?>
  37.             <?php comment_date('F jS, Y') ?> <?php comment_time() ?> <?php edit_comment_link('Edit','',''); ?>
  38.  
  39.             <div class="commenttext">
  40.             <?php comment_text() ?>
  41.             </div>
  42.  
  43.         </li>
  44.  
  45.     <?php /* Changes every other comment to a different class */
  46.         if ('alt' == $oddcomment) $oddcomment = '';
  47.         else $oddcomment = 'alt';
  48.     ?>
  49.  
  50.      <?php } else { $i_have_trackbacks = true; } ?>
  51.  
  52.     <?php endforeach; /* end for each comment */ ?>
  53.  
  54.     </ol>
  55.  
  56.  <?php else : // this is displayed if there are no comments so far ?>
  57.  
  58.     <?php if ('open' == $post->comment_status) : ?>
  59.         <!-- If comments are open, but there are no comments. -->
  60.  
  61.      <?php else : // comments are closed ?>
  62.         <!-- If comments are closed. -->
  63.         <p class="nocomments">Comments are closed.</p></div>
  64.  
  65.     <?php endif; ?>
  66. <?php endif; ?>
  67.  
  68.  
  69. <?php if ('open' == $post->comment_status) : ?>
  70.  
  71. <p id="respond"><b>Leave a Reply</b></p>
  72.  
  73. <?php if ( get_option('comment_registration') && !$user_ID ) : ?>
  74. <p>You must be <a href="<?php echo get_option('siteurl'); ?>/wp-login.php?redirect_to=<?php the_permalink(); ?>">logged in</a> to post a comment.</p></div>
  75. <?php else : ?>
  76.  
  77. <form action="<?php echo get_option('siteurl'); ?>/wp-comments-post.php" method="post" id="commentform">
  78.  
  79. <?php if ( $user_ID ) : ?>
  80.  
  81. <p>Logged in as <a href="<?php echo get_option('siteurl'); ?>/wp-admin/profile.php"><?php echo $user_identity; ?></a>. <a href="<?php echo get_option('siteurl'); ?>/wp-login.php?action=logout" title="Log out of this account">Logout &raquo;</a></p>
  82.  
  83. <?php else : ?>
  84.  
  85. <p><label for="author">Name <?php if ($req) echo "(required)"; ?></label><br />
  86. <input type="text" name="author" id="author" value="<?php echo $comment_author; ?>" size="40" tabindex="1" /></p>
  87.  
  88. <p><label for="email">Email Address<?php if ($req) echo "(required)"; ?></label><br />
  89. <input type="text" name="email" id="email" value="<?php echo $comment_author_email; ?>" size="40" tabindex="2" /></p>
  90.  
  91. <p><label for="url">Website</label><br />
  92. <input type="text" name="url" id="url" value="<?php echo $comment_author_url; ?>" size="40" tabindex="3" /></p>
  93.  
  94. <?php endif; ?>
  95.  
  96. <!--<p><small><strong>XHTML:</strong> You can use these tags: <?php echo allowed_tags(); ?></small></p>-->
  97.  
  98. <p><textarea name="comment" id="comment" cols="50" rows="10" tabindex="4"></textarea></p>
  99.  
  100. <p><input name="submit" type="submit" id="submit" tabindex="5" value="Submit Comment" />
  101. <input type="hidden" name="comment_post_ID" value="<?php echo $id; ?>" /></p>
  102.  
  103. <?php do_action('comment_form', $post->ID); ?>
  104.  
  105. </form>
  106.  
  107. <?php if($i_have_trackbacks) { ?>
  108.  
  109. <h3>Trackbacks</h3>
  110.  
  111.      <ol class="commentlist">
  112.  
  113.     <?php foreach ($comments as $comment) : ?>
  114.    
  115.     <?php if (get_comment_type() != "comment") { ?>
  116.  
  117.         <li class="<?php echo $oddcomment; ?>" id="comment-<?php comment_ID() ?>">
  118.             <?php comment_author_link() ?> on
  119.             <?php if ($comment->comment_approved == '0') : ?>
  120.             <em>Your comment is awaiting moderation.</em>
  121.             <?php endif; ?>
  122.             <?php comment_date('F jS, Y') ?> <?php comment_time() ?> <?php edit_comment_link('Edit','',''); ?>
  123.  
  124.             <div class="commenttext">
  125.             <?php comment_text() ?>
  126.             </div>
  127.  
  128.         </li>
  129.  
  130.     <?php /* Changes every other comment to a different class */
  131.         if ('alt' == $oddcomment) $oddcomment = '';
  132.         else $oddcomment = 'alt';
  133.     ?>
  134.    
  135.     <?php } ?>
  136.  
  137.     <?php endforeach; /* end for each trackback */ ?>
  138.  
  139.      </ol>
  140.  
  141. <?php } ?>
  142.  
  143. </div>
  144.  
  145. <?php endif; // If registration required and not logged in ?>
  146.  
  147. <?php endif; // if you delete this the sky will fall on your head ?>

And provides a more user friendly way of displaying comments and trackbacks.

Rate this post: 1 Star2 Stars3 Stars4 Stars5 Stars (10 votes, average: 3.4 out of 5)
Loading ... Loading ...
If you found this post useful, please consider a small donation.
» WordPress themes are designs for WordPress - one of the most popular blogging software nowadays.
You will be pleasantly surprised by WordPress Themes provided by Template Monster. All of them are of professional design and high quality.

2 Responses to “Separate comments from trackbacks in your WordPress blog”

  1. MichaelJWilliams on September 10th, 2008 3:42 pm

    This is really useful; thanks Emanuele!

  2. Adam Gunderson on September 21st, 2008 10:13 pm

    Thank you. I was wondering how to do this for a long time. Very good work!

Leave a Reply




Posts