Free Wordpress Blog Setup

How to Separate Trackbacks from Comments in WordPress

By Editorial Staff in Tutorials
How to Separate Trackbacks from Comments in WordPress

How to Separate Trackbacks from Comments is no new hack, but when WordPress released version 2.7, they introduced a new renovated comment system which included threaded comments, ability to paginate and much more. But along with this change, they also changed a lot of core file parameters. In this article we will show you how you can separate trackbacks from comments in WordPress. This hack will only work for version 2.7+ and if you are not using it, then you should start now because of the recent MySQL attack on older versions.

We found this tutorial on one of the WordPress developer’s website called Sivel.net

Here is an example of the new loop that we will be referring to in the tutorial:

<?php if ( have_comments() ) : ?>
<h3 id="comments"><?php comments_number('No Responses', 'One Response', '% Responses' );?> to %u201C<?php the_title(); ?>%u201D</h3>
<ol class="commentlist">
<?php wp_list_comments(); ?>
</ol>
<div class="navigation">
<div class="alignleft"><?php previous_comments_link() ?></div>
<div class="alignright"><?php next_comments_link() ?></div>
</div>

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

<?php if ('open' == $post->comment_status) : ?>
<!– If comments are open, but there are no comments. –>

<?php else : // comments are closed ?>
<!– If comments are closed. –>
<p class="nocomments">Comments are closed.

<?php endif; ?>
<?php endif; ?>

Find this code in your comments.php:

<?php if ( have_comments() ) : ?>

Directly below this code add the following code:

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

Once you have added the above code then find this code:

<?php wp_list_comments(); ?>

Replace the above code with the following code:

<?php wp_list_comments('type=comment'); ?>

Now as you see in our example loop there is a code for ordered list that looks like

</ol>

Directly below this code add:

<?php endif; ?>

Now by adding the endif tag, if you do not have any comments, the ordered list will not be displayed. Now lets move on to adding the pings to the comments.

Add the following code below or however you want to display it. It will display the pings.

<?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; ?>

Now when you have this it will display the trackbacks but it will show them just like comments. Now you may want to display them as a list because other wise you are just wasting space. So here is how you can do that.

Simply open functions.php which is in your themes folder and add the following function in there:

<?php
function list_pings($comment, $args, $depth) {
$GLOBALS['comment'] = $comment;
?>
<li id="comment-<?php comment_ID(); ?>"><?php comment_author_link(); ?>
<?php } ?>

This function will let you display the pings as a list instead of showing like a comment. But you have to do one more thing.

Open your comments.php and find this code:

<ol>
<?php wp_list_comments('type=pings'); ?>

Replace it with:

<ol>
<?php wp_list_comments('type=pings&callback=list_pings'); ?>

Now the final copy of the example loop would look this:

<?php if ( have_comments() ) : ?>
<?php if ( ! empty($comments_by_type['comment']) ) : ?>
<h3 id="comments"><?php comments_number('No Responses', 'One Response', '% Responses' );?> to %u201C<?php the_title(); ?>%u201D</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="pinglist">
<?php wp_list_comments('type=pings&callback=list_pings'); ?>
</ol>

<?php endif; ?>
<div class="navigation">
<div class="alignleft"><?php previous_comments_link() ?></div>
<div class="alignright"><?php next_comments_link() ?></div>
</div>

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

<?php if ('open' == $post->comment_status) : ?>
<!– If comments are open, but there are no comments. –>

<?php else : // comments are closed ?>
<!– If comments are closed. –>
<p class="nocomments">Comments are closed.

<?php endif; ?>
<?php endif; ?>

Now you are done. There is a bonus hack that you can use. Since you are displaying them separately, it would be good if you display the accurate comment count by excluding trackbacks and pings. Use this tutorial that we wrote to display the most accurate comment count in WordPress.

What Next?

Digg it
Save This Page
Subscribe to WPBeginner
Stumble it
Free Wordpress Blog Setup

Comments

4 Responses to “How to Separate Trackbacks from Comments in WordPress”
  1. The url of this site wpbeginner.com. But I think the tutorials are kind of advanced. Keep up the good work.

  2. twthosting says:

    great! all expert start from beginner, so this can be one of their kick start. thanks!

  3. Jesse says:

    Thanks, This is exactly what I want!

  4. Laura says:

    Thanks for sharing this! is exactly what I was looking for.

Share Your Opinions

Tell us what you're thinking...
and if you want a pic to show with your comment, then get gravatar!

Please make sure that you have read our Comment Policy.

Due to high volume of request from our readers, we are adding this feature that allows you to stay updated with this post's comments without having to participate in the discussion even though we would love your input as always. Don't worry we hate SPAM just as much as you do, so you will never receive any SPAM messages from our site and that's our promise to you.

Subscribe without commenting

Close Bar