HOWTO: Separare commenti e trackbacks in WordPress | Flavio's blog
 

HOWTO: Separare commenti e trackbacks in WordPress

Few days ago we have seen how to make sure that WordPress does not count the number of trackbacks in comments to the post.

Now we want to separate, at the end of the post dedicated to user feedback, REAL comments and trackbacks / pingbacks, according to this model:

post

————

Comments (NUMBER COMMENTS)

TRACKBACKS (NUMERO TRACKBACKS)

In this way the blog will be more clean and orderly.

Let's open the file functions.php of your wordpress theme, and insert this code:

//funzione per contare il numero di trackbacks e pingbacks
function trackback_count() {
global $wpdb;
$post_id = get_the_ID();
$post_ping_count = $wpdb->get_var("SELECT count(comment_id) FROM $wpdb->comments WHERE comment_type in ('pingback',
'trackback') and comment_approved = 1 and comment_post_id = $post_id");
echo $post_ping_count;
}

This code extrapolates the number of trackbacks and pingbacks from the total number of general comments, so that we can know HOW MANY trackbacks has our post.

then, inspired by Problogdesign.com, let's change the way the template shows our comments.

We open the file comments.php and look for the line

<?php foreach ($comments as $comment) : ?>

and soon after we go to paste this code:

<?php $comment_type = get_comment_type(); ?>
<?php if($comment_type == 'comment') { ?>

Now we look for the line:

<?php endforeach; /* end for each comment */ ?>

and just before we insert this code:

<?php } else { $trackback = true; } /* End of is_comment statement */ ?>

In this way, the comments will no longer display trackbacks.

Now let's show them AFTER the part devoted to comments.
Let the line

<?php else : // this is displayed if there are no comments so far ?>

and just before we go to paste this code:

<br/>
<h3><?php trackback_count(); ?> Trackbacks/Pingbacks</h3>
<ol>
<?php foreach ($comments as $comment) : ?>
<?php $comment_type = get_comment_type(); ?>
<?php if($comment_type != 'comment') { ?>
<LI><?php comment_author_link() ?></LI>
<?php } ?>
<?php endforeach; ?>
</ol>

This will show the number of trackbacks and pingbacks, and it will list, just like at the end of this post.

Chuinque has used this code, and have questions or comments, leave us a comment below with your own experience!

 

1 Comment »

Please accept third-party cookies to be able to comment on the post! The CHANGE COOKIE CHOICES button is located in the footer of the site. / In order to comment this post, please accept the third party cookies! The button CAMBIA LE SCELTE DEI COOKIE is in the footer of the website.