21 Jun, 2004
Separating Comments and Trackbacks
Posted by: Jennifer In: WordPress my-hacks additions
Some habits are hard to break. In the MT world – comments and trackbacks are different animals. Maybe just because I'm used to it that way – but that's the way I prefer it. A trackback/pingback isn't a complete thought. Comments (usually) are. When reading the comments section, I find it disrupting to see a trackback in the middle of the discussion. When I'm ready to leave the site and read another opinion, I will.
Ok, I'll get off the soapbox now. Greg asked me how I separated the comments and trackbacks on this site. Here's how:
Add the following function to your my-hacks.php file (See more information on how to use my-hacks.php here – link via Carthik) :
function return_comment_type($commenttxt = 'Comment', $trackbacktxt = 'Trackback', $pingbacktxt = 'Pingback') {
global $comment;
if (preg_match('|<trackback />|', $comment->comment_content))
return $trackbacktxt;
elseif (preg_match('|<pingback />|', $comment->comment_content))
return $pingbacktxt;
else
return $commenttxt;
}
And here's how you'll use it…
In wp-comments.php – this is what I have (edit for your own use as neccessary)
<?php if ($comments) { ?>
<?php
$cmts = 0;
$trkbks = 0;
?>
<p><b>:: Trackbacks/Pingbacks :: </b></p>
<ol>
<?php foreach ($comments as $comment) { ?>
<?php if (return_comment_type() != "Comment") { ?>
<?php $trkbks++; ?>
<a name="comment-<?php comment_ID() ?>"></a>
<li><?php comment_text() ?>
<p><?php comment_type(); ?> <?php _e("by"); ?> <?php comment_author_link() ?> — <?php comment_date() ?> <?php comment_time() ?> | <a href="#comment-<?php comment_ID() ?>">permalink</a></cite> <?php edit_comment_link(__("Edit This"), ' |'); ?></p>
<br />
</li>
<?php } ?>
<?php } // end for each comment ?>
</ol>
<?php if($trkbks <= 0) { ?>
<p>No Trackbacks/Pingbacks</p>
<?php } ?>
<p><b>:: Comments :: </b></p>
<ol>
<?php foreach ($comments as $comment) { ?>
<?php if (return_comment_type() == "Comment") { ?>
<?php $cmts++; ?>
<a name="comment-<?php comment_ID() ?>"></a>
<?php
// this is actually the "important" comment highlightling
$isImportant = false;
if (stristr($comment->comment_content, "<!–important–>")) {
$isImportant = true;
} ?>
<li>
<?php if($isImportant) { echo '<div class="importantcomment">';} ?>
<?php comment_text() ?>
<p><?php comment_type(); ?> <?php _e("by"); ?> <?php comment_author_link() ?> — <?php comment_date() ?> <?php comment_time() ?> | <a href="#comment-<?php comment_ID() ?>">permalink</a> <?php edit_comment_link(__("Edit This"), ' |'); ?></p>
<?php if($isImportant) { echo '</div>';} ?>
<br />
</li>
<?php } ?>
<?php } // end for each comment ?>
</ol>
<?php if($cmts <= 0) { ?>
<p>No comments</p>
<?php } ?>
<?php } else { // this is displayed if there are no comments so far ?>
<p><?php _e("No comments yet."); ?></p>
<?php } ?>
I should add that I'm not making any guarantees that this code will validate – feel free to edit for your own purpose.
(Updated to add: I'll probably want to combine this with this plugin I just found (via WxGal) which will give you a separate count for trackbacks and comments)
Post last updated: 6/22/04 9:27pm