Separating WordPress comments from pings and trackbacks

Aug 9, 2007 | Tags: , , , , | Written by Administrator

Alternative separation method

As with most things there are several alternatives, and a popular technique is to filter the pings. This requires less PHP code, but I also find it less flexible, and it needs extra work to show comment and ping counts.

First we modify the comments section so it looks like this:

  1. <?php if ($comments) : ?>
  2.   <h3 id="comments">
  3.     <?php comments_number('No Responses','One Response','% Responses');?>
  4.     to &#8220;<?php the_title(); ?>&#8221;
  5.   </h3>
  6.  
  7.   <ol class="commentlist">
  8.  
  9.   <?php foreach ($comments as $comment) : ?>
  10.     <?php if (comment_type == 'comment') : ?>
  11.     <li <?php echo $oddcomment; ?>id="comment-<?php comment_ID() ?>">
  12.       <cite><?php comment_author_link() ?></cite> Says:
  13.       <?php if ($comment->comment_approved == '0') : ?>
  14.       <em>Your comment is awaiting moderation.</em>
  15.       <?php endif; ?>
  16.       <br />
  17.  
  18.       <small class="commentmetadata">
  19.         <a href="#comment-<?php comment_ID() ?>" title="">
  20.           <?php comment_date('F jS, Y') ?> at <?php comment_time() ?>
  21.         </a>
  22.         <?php edit_comment_link('edit','&nbsp;&nbsp;',''); ?>
  23.       </small>
  24.  
  25.       <?php comment_text() ?>
  26.  
  27.     </li>
  28.     <?php endif; ?>
  29.   <?php
  30.     /* Changes every other comment to a different class */
  31.     $oddcomment = ( empty( $oddcomment ) ) ? 'class="alt" ' : '';
  32.   ?>
  33.  
  34.   <?php endforeach; /* end for each comment */ ?>
  35.  
  36.   </ol>
  37.  
  38.  <?php else : // this is displayed if there are no comments so far ?>
  39.  
  40.   <?php if ('open' == $post->comment_status) : ?>
  41.     <!-- If comments are open, but there are no comments. -->
  42.  
  43.    <?php else : // comments are closed ?>
  44.     <!-- If comments are closed. -->
  45.     <p class="nocomments">Comments are closed.</p>
  46.  
  47.   <?php endif; ?>
  48. <?php endif; ?>

What's happening here is that we are stepping through all our comments as normal, but before we display a comment we check the type (line 10 and 28). If the type is 'comment' we display it, otherwise we assume it is a ping or trackback and move on.

The pings themselves are then displayed with:

  1. <?php if ($comments) : ?>
  2.   <h3 id="pings">Pings</h3>
  3.  
  4.   <ol class="commentlist">
  5.  
  6.   <?php foreach ($comments as $comment) : ?>
  7.     <?php if (comment_type != 'comment') : ?>
  8.     <li <?php echo $oddcomment; ?>id="comment-<?php comment_ID() ?>">
  9.       <cite><?php comment_author_link() ?></cite> Says:
  10.  
  11.       <br />
  12.  
  13.       <small class="commentmetadata">
  14.         <a href="#comment-<?php comment_ID() ?>" title="">
  15.           <?php comment_date('F jS, Y') ?> at <?php comment_time() ?>
  16.         </a>
  17.         <?php edit_comment_link('edit','&nbsp;&nbsp;',''); ?>
  18.       </small>
  19.  
  20.       <?php comment_text() ?>
  21.  
  22.     </li>
  23.     <?php endif; ?>
  24.   <?php
  25.     /* Changes every other comment to a different class */
  26.     $oddcomment = ( empty( $oddcomment ) ) ? 'class="alt" ' : '';
  27.   ?>
  28.  
  29.   <?php endforeach; /* end for each comment */ ?>
  30.  
  31.   </ol>
  32.   <?php endif; ?>
  33. <?php endif; ?>

This is the reverse of the previous code and as we step through the comments we are only displaying comments whose type is not 'comment' (line 7 and 23) - all the pings and trackbacks.

Resources

Please direct all support questions to the support forum.

Share This

Comments

Comments are shown on the first page.

Home | Software | Terms & Conditions | Sitemap | John Godley © 2008
Close
E-mail It