My improved Pingbacks/Trackbacks & Comments separation for WordPress 2.0

Rachel Runcliffe posted a topic on separating Pingbaks/Trackbacks from Comments in WordPress 2.0 … now, I’ve improved this thingy by implementing a third loop which first counts the number of Pingbacks/Trackbacks & Comments so that the HTML still remains valid. Anywho, here goes

Procedure to install this still remains the same: edit the comments template of your current wp-theme. Copy the piece of code below and paste it over the current comments loop…

<?php if ($comments) : ?>

<?php /* Count the totals */
$numPingBacks = 0;
$numComments  = 0;

foreach ($comments as $comment) {
if (get_comment_type() != "comment") {
$numPingBacks++;
} else {
$numComments++;
}
}
?>

<?php if ($numPingBacks != 0) : ?>
<h3 id="trackbacks">Trackbacks/Pings</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() ?>">
<cite>
<span class="author"><?php comment_author_link() ?></span>
<span class="date"><?php comment_date('M d Y') ?> / <?php comment_date('ga') ?></span>
</cite>

<div class="content">
<?php if ($comment->comment_approved == '0') : ?>
<em>Your comment is awaiting moderation.</em>
<?php endif; ?>
<?php comment_text() ?>
</div>
<div class="clear"></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 endif; ?>

<?php if ($numComments != 0) : ?>
<h3 id="comments">Comments</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() ?>">
<cite>
<span class="author"><?php comment_author_link() ?></span>
<span class="date"><?php comment_date('M d Y') ?> / <?php comment_date('ga') ?></span>
</cite>

<div class="content">
<?php if ($comment->comment_approved == '0') : ?>
<em>Your comment is awaiting moderation.</em>
<?php endif; ?>
<?php comment_text() ?>
</div>
<div class="clear"></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 endif; ?>
<?php else : // this is displayed if there are no comments so far ?>

Happy copy-n-pasting!

Published by Bramus!

Bramus is a frontend web developer from Belgium, working as a Chrome Developer Relations Engineer at Google. From the moment he discovered view-source at the age of 14 (way back in 1997), he fell in love with the web and has been tinkering with it ever since (more …)

Unless noted otherwise, the contents of this post are licensed under the Creative Commons Attribution 4.0 License and code samples are licensed under the MIT License

Join the Conversation

6 Comments

  1. Hey,

    Nice fix! 🙂

    Just one little problem with this bad boy, it shows pings/trackbacks as comments on the home page. That is it shows for example, “three are 3 comments” . When in fact there is 1 trackback and two comments. 🙁

    Any idea on how to fix?

    Thanks,

    Will

Leave a comment

Leave a Reply to manuel Cancel reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.