Separating WordPress comments from pings and trackbacks
How comments work
First let's look at how comments work. If you are unfamiliar with WordPress themes then it may be worth taking a look at a theme guide to get a general overview, but it's not important. All examples will be shown using the default WordPress theme (Kubrick), and all named files can be found in the default theme directory (/wp-content/themes/default/). Note that you are advised to make a copy of any files you modify
Comments are generally only displayed on full post pages, and not on the home page or any of the archive pages. In a WordPress theme the full-post page is defined by the file single.php. Tucked away at the bottom of this file is a simple function call:
<?php comments_template(); ?>
This is where all the magic happens. According to the WordPress Codex:
This tag includes the file comments.php from your current theme's directory.
Looking at comments.php we find a big old mess of code. Without spending too much time looking at the details of what every line does, the part that is important for this guide can be extracted as shown:
<?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) : ?> <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 /* 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; ?>
Remember that anything between <?php and ?> is a PHP function, with everything else being HTML. Looking at the code the whole section is wrapped in a conditional statement:
<?php if ($comments) : ?> CODE <?php endif; ?>
This checks if the $comments variable exists (in PHP a variable starts with a dollar). The $comments variable is setup for us by WordPress and we don't need to worry about where it comes from. If it does exist then the comments are displayed, otherwise nothing is shown.
Further inside the code we find a loop:
<?php foreach ($comments as $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 /* Changes every other comment to a different class */ $oddcomment = ( empty( $oddcomment ) ) ? 'class="alt" ' : ''; ?> <?php endforeach; /* end for each comment */ ?>
This is a PHP looping construct that looks at each item in $comments in turn and assigns it to $comment (singular).
What have we learnt from all this? The most important fact is that comments are displayed in comments.php, and that a variable exists called $comments which contains all our comments and pings. This is almost everything we need to know to separate comments and pings.
Please direct all support questions to the support forum.






Comments
Comments are shown on the first page.