Separating WordPress comments from pings and trackbacks
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:
- <?php if ($comments) : ?>
- <h3 id="comments">
- <?php comments_number('No Responses','One Response','% Responses');?>
- to “<?php the_title(); ?>”
- </h3>
- <ol class="commentlist">
- <?php foreach ($comments as $comment) : ?>
- <?php if (comment_type == 'comment') : ?>
- <li <?php echo $oddcomment; ?>id="comment-<?php comment_ID() ?>">
- <cite><?php comment_author_link() ?></cite> Says:
- <?php if ($comment->comment_approved == '0') : ?>
- <em>Your comment is awaiting moderation.</em>
- <?php endif; ?>
- <br />
- <small class="commentmetadata">
- <a href="#comment-<?php comment_ID() ?>" title="">
- <?php comment_date('F jS, Y') ?> at <?php comment_time() ?>
- </a>
- <?php edit_comment_link('edit',' ',''); ?>
- </small>
- <?php comment_text() ?>
- </li>
- <?php endif; ?>
- <?php
- /* Changes every other comment to a different class */
- $oddcomment = ( empty( $oddcomment ) ) ? 'class="alt" ' : '';
- ?>
- <?php endforeach; /* end for each comment */ ?>
- </ol>
- <?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.</p>
- <?php endif; ?>
- <?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:
- <?php if ($comments) : ?>
- <h3 id="pings">Pings</h3>
- <ol class="commentlist">
- <?php foreach ($comments as $comment) : ?>
- <?php if (comment_type != 'comment') : ?>
- <li <?php echo $oddcomment; ?>id="comment-<?php comment_ID() ?>">
- <cite><?php comment_author_link() ?></cite> Says:
- <br />
- <small class="commentmetadata">
- <a href="#comment-<?php comment_ID() ?>" title="">
- <?php comment_date('F jS, Y') ?> at <?php comment_time() ?>
- </a>
- <?php edit_comment_link('edit',' ',''); ?>
- </small>
- <?php comment_text() ?>
- </li>
- <?php endif; ?>
- <?php
- /* Changes every other comment to a different class */
- $oddcomment = ( empty( $oddcomment ) ) ? 'class="alt" ' : '';
- ?>
- <?php endforeach; /* end for each comment */ ?>
- </ol>
- <?php endif; ?>
- <?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
- Separate Comments Plugin
- Pre-modified
comments.phpfor default theme (requires above plugin)
Please direct all support questions to the support forum.






Comments
Comments are shown on the first page.