Wordpress: How to separate comments from pingbacks
March 7, 2007 | Comments (4) | Filed under: Tutorials
As a fairly recent newcomer to Wordpress, I’ve always been confused about their decision to inter-mix regular comments (ie. from visitors to your site) and pingbacks (ie. automated comments indicating someone has linked you post). If you’ve spent any time visiting Wordpress blogs, you’re guaranteed to have come across a comment post like this one:
11. links for 2007-03-07 « HAPHT @ March 07, 2007 at 2:24 pm
[…] » 50 sources for web design inspiration by Bookmark Bliss (tags: web-design) […]
These automated pingback posts are great, since they give you an indication of just how far your articles have spread and which sites are interested in your content. At the same time though, I find they seriously interrupt the flow of your comments and on a site that gets a lot of pingbacks, can almost make it impossible to follow a comment thread consistently.
Since this blog is fairly new, I thought I would figure out how to separate the pingbacks from the regular comments and place them under two different headings. As it turns out, it’s fairly easy to do with a few modifications to your theme and no changes to the core Wordpress files. Here is what you need to do:
First, open up the comments.php file for your template. Every theme is a little different, but there are commonalities to each that should be consistent. Inside your file, you should be able to find a line that reads:
<?php if ($comments) : ?>
This line is the start of the comments section of your post. Immediately underneath this line, we want to step through our comments and split them into two sections. There are a variety of ways you can do this, but here is how I did it.
<?php
$pingBacks = array( );
$commentList = array( );foreach( $comments as $comment ) {
if( get_comment_type( ) == “pingback” ) {
array_push( $pingBacks, $comment );
} else {
array_push( $commentList, $comment );
}
}
?>
The only new piece of code you might not be familiar with here is the get_comment_type( ) call. This call simply checks the current comment and returns whether or not it is a comment or pingback. Once you have the two sets of comments separated, all that’s left is to print them both out in whatever style you choose. For an example, this is how we do it on our site:
<?php foreach ($commentList as $comment) : ?>
<div id=”comment-<?php comment_ID() ?>”>
<h4>
<img src=”images/user_comment.png” class=”contentimage” /><?php comment_author_link() ?> on
<?php comment_date(’F d, Y’) ?> at
<?php comment_time() ?><p><?php comment_text() ?></p>
</h4>
</div><?php endforeach; ?>
<?php foreach ($pingBacks as $comment) : ?>
<div id=”comment-<?php comment_ID() ?>”>
<h4>
<img src=”images/user_comment.png” class=”contentimage” /><?php comment_author_link() ?> on
<?php comment_date(’F d, Y’) ?> at
<?php comment_time() ?></h4>
</div>
<?php endforeach; ?>
All of the above code is standard for most Wordpress themes. It is modified slightly to work with the template at Bookmark Bliss, but you can easily make changes to match your own. Once you’ve made these additions, you’re set! If your interested in seeing an example of these changes in action, check out our 50 Sources for Web Design Inspiration post and look at the responses section at the bottom of the page.
Not the most elegant solution by a long shot, but it gets the job done nonetheless. If you have any questions or suggestions on this example, let us know.
feel free to leave a comment
Comment Guidelines: Basic XHTML is allowed (a href, strong, em, code). All line breaks and paragraphs are automatically generated. Off-topic or inappropriate comments will be edited or deleted. Email addresses will never be published. Keep it PG-13 people!
XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>
All fields marked with " * " are required.



4 people have left comments
It would be really nice to have it as a plugin.
Let me see whether I get some time so that I can package it into a nice plugin.
Cheers,
Sudar