Beginner's Guide for WordPress - Start your WordPress Blog in minutes.
Choosing the Best
WordPress Hosting
How to Easily
Install WordPress
Recommended
WordPress Plugins
View all Guides

How to Separate Trackbacks from Comments in WordPress

Last updated on by
Follow WPBeginner on YouTube
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']) ) : ?>
[/php

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

1
<?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.


Editorial Staff at WPBeginner is a team of WordPress lovers led by Syed Balkhi. Page maintained by Syed Balkhi.

WPBeginner's Video Icon
Our HD-Quality tutorial videos for WordPress Beginners will teach you how to use WordPress to create and manage your own website in about an hour. Get started now »
  • Iteire Apollos

    i use genesis theme and i want to separate trackbacks and ping-backs from comments. Do you have any idea how i can go about it?

    • http://www.wpbeginner.com Editorial Staff

      Genesis themes by default does that.

  • http://webtolerant.com Amey

    Nice post. As an add, you should also close the tag used in the “comments are closed” section.

  • Laura

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

  • http://newwebzen.com Jesse

    Thanks, This is exactly what I want!

  • http://twthosting.com twthosting

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

  • http://www.comm100.com/livechat/ Tim Live Chat

    The url of this site wpbeginner.com. But I think the tutorials are kind of advanced. Keep up the good work.