Separate Pingbacks from User Comments

wordpress commentsPeople have predicted their death for a long time, but pingbacks/trackbacks (and displaying them) are still firmly with us – there is a certain vanity involved…

They do give an indication of related content, other sites in your niche (and just how many automated spam sites there are out there ) etc. At the same time, they break up the flow of user comments and even with threaded comments can make it difficult for visitors to follow the discussion.

So it’s reasonable to separate out pingbacks and perhaps not include their digest content, just the title and a link. The method has changed for WordPress 2.7 upwards, with the arrival of threaded comments and their integration into the core code.

The frist step is to adjust the call to the comments template in single.php or where ever you’re hoping to display the comments… Replace:

<?php comments_template(); ?>

with

<?php comments_template('', true); ?>

The “true” flag gives access to an array $comments_by_type – as the name might even suggest, a record of the numbers of each type of comment for that post.

Your theme will (should) have something like this code in it – most likely in comments.php but possibly as a separate comments template function in functions.php or elsewhere – the important thing is to locate comments_number() and wp_list_comments()

<h3 id="comments"><?php comments_number('No Responses', 'One Response', '% Responses' );?> to &#8220;<?php the_title(); ?>&#8221;</h3>

<ol class="commentlist">
<?php wp_list_comments(); ?>
</ol>

Replace it with this:


<?php if ( !empty($comments_by_type['comment']) ) : ?>

<h3 id="comments"><?php comments_number('No Responses', 'One Response', '% Responses' );?> to &#8220;<?php the_title(); ?>&#8221;</h3>

<ol class="commentlist">
<?php wp_list_comments('type=comment'); ?>
</ol>

<?php endif; ?>

<?php if ( ! empty($comments_by_type['pings']) ) : ?>

<h3 id="pings">Trackbacks/Pingbacks</h3>
<ol class="commentlist">
<?php wp_list_comments('type=pings'); ?>
</ol>

<?php endif; ?>

We have 2 lists now, created by wp_list_comments

The other (optional) issue is to recalculate the headline figure of comments – which is still showing the total of user comments + pings. This is done with the use of the get_comments_number filter. Add, in functions.php

<?php

add_filter('get_comments_number', 'comment_count', 0);
function comment_count( $count ) {
if ( ! is_admin() ) {
global $id;
$comments_by_type = &separate_comments(get_comments('status=approve&post_id=' . $id));
return count($comments_by_type['comment']);
} else {
return $count;
}
}
?>

And that should be that – next step would be to restyle the two lists, with a customized callback function for wp_list_comments() – but that will have to be for another post…

Share:
  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • email
  • LinkedIn
  • Reddit
  • StumbleUpon
  • RSS
  • Twitter

One Response to “Separate Pingbacks from User Comments”

  1. Dauren Yet says:

    Your method ping each comment?

Leave a Reply