Separating Trackbacks/Pings from Comments in WordPress

Standard

I was trying to figure out the best way to separate trackbacks/pings from the comments here on the site. I’ve seen a number of tutorials out there, but none seemed to do exactly what I wanted. The code is piece-mealed from a couple of places, but this is how I accomplished it here on the site:

Backup your existing comments.php file associated with your theme! I cannot stress this enough.

Assuming you’re using the default theme, or some variation thereof, you’re going to want to look for this in your comments.php file:

<!-- You can start editing here. -->
< ?php if ($comments) : ?>

You’re basically going to replace the logic on comments.php from where it says < ?php if ($comments) : ?> (Line 20) to < ?php endif; ?> (Line 61) with the logic below.

Define the number of pings and comments (Source: Sandbox theme):

< ?php /* NUMBERS OF PINGS AND COMMENTS */
$ping_count = $comment_count = 0;
foreach ( $comments as $comment )
get_comment_type() == "comment" ? ++$comment_count : ++$ping_count;
?>

Then check to see whether comment_status is set to 'open'. If it is 'open', we then check to make sure the comment_count isn’t equal to zero. Once these conditions are met, I then display only the comments. Some other conditional logic is included, like checking whether the comment’s being held for moderation:

< ?php if ('open' == $post-> comment_status) : ?>
< ?php if ( $comment_count ) : ?>
< ?php if ($comments) : ?>
<!-- Comments! -->
<h3>Stuff said in regard to this entry:</h3>
<ol class="commentlist">
< ?php foreach ($comments as $comment) : ?>
< ?php if (get_comment_type() == "comment"){ ?>
<li class="<?php echo $oddcomment; ?>" id="comment-< ?php comment_ID() ?>">
<div class="gravatar"><img src="<?php gravatar("R", 40); ?/>" alt="" /></div>
<cite>< ?php comment_author_link() ?> < ?php _e('said'); ?>:</cite>
<div class="comment-text">
< ?php comment_text() ?>

< ?php if ($comment->comment_approved == '0') : ?>
<p class="unapproved"><em>Your comment is awaiting moderation.</em></p>
< ?php endif; ?>

<p class="comments-meta">
Posted <span class="date">< ?php comment_date('F j, Y') ?></span> @
<span class="permalink"><a href="#comment-<?php comment_ID() ?>" title="< ?php _e('Permanent link to this comment'); ?>">< ?php comment_time() ?></a></span>
< ?php edit_comment_link(__("Edit"), '<span class="edit"> ', ''); ?>
</p>
</div>
</li>
< ?php /* Changes every other comment to a different class */
if ('alt' == $oddcomment) $oddcomment = '';
else $oddcomment = 'alt';
?>
< ?php } ?>
< ?php endforeach; /* end for each comment */ ?>
</ol>
< ?php else : ?>
< ?php endif ?>

< ?php else : ?>
<h3>Hey, there's no comments on this post!</h3>
<p><a href="#respond">How about being the first?</a></p>

< ?php endif /* if ( $comment_count ) */ ?>

< ?php else : // comments are closed ?>

< ?php /* Comments are closed */ ?>
<h3>Sorry dude, comments are closed on this post. If you had stuff you liked to say about this entry,
<a href="/contact/">feel free to contact me</a>.</h3>

< ?php endif; /* if ('open' == $post-> comment_status) */ ?>

Then we do the same thing for trackback/pings:

< ?php if ( pings_open() ) : ?>
< ?php if ( $ping_count ) : ?>
<!-- Pings! -->
<h3 id="trackbacks">Trackback/Ping Stuff:</h3>
<ol class="pinglist">
< ?php foreach ($comments as $comment) : ?>
< ?php if (get_comment_type() != "comment") : ?>
<li class="<?php echo $oddcomment; ?> smaller" id="comment-< ?php comment_ID() ?>">
<cite>< ?php comment_author_link() ?></cite>
ยท <span class="commentmetadata">< ?php comment_date('F jS, Y') ?> at < ?php comment_time() ?> < ?php edit_comment_link('e','<span class="edit"> ', '</span>'); ?>
</li>
< ?php
/* Changes every other comment to a different class */
if ('alt' == $oddcomment) $oddcomment = '';
else $oddcomment = 'alt';
?>
< ?php endif; ?>
< ?php endforeach; /* end for each comment */ ?>
</ol>
< ?php else : /* if ( $ping_count ) */ ?>

< ?php endif /* if ( $ping_count ) */ ?>

< ?php else : // pings are a no-go bro. ?>

< ?php /* Pings are a no-go, bro. */ ?>
<h3>Pings are a no-go, bro.</h3>
<p>Maybe next time?</p>

< ?php endif /* if ( pings_open() ) */ ?>

Save the changes and you should be good to go.

Hopefully this is a pretty straightforward implementation… If you have any better ideas, I’d love to hear ‘em.

6 thoughts on “Separating Trackbacks/Pings from Comments in WordPress

Comments are closed.