With all the WordPress themes available to WordPress users, it always surprises me how these incredible theme authors don’t take a few extra seconds to separate their theme’s trackbacks from the comments. It doesn’t look very professional and it can make it extremely difficult to follow a conversation in the comments.

Separating your trackbacks and comments requires a minimal amount of coding work to set up. First, you’ll want to make a backup of your comments.php file just in case something goes wrong. Next, follow these three steps:

1 ) Access your comments.php file and locate the following code:

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

Immediately after the above code, you’ll want to place this code:

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

2 ) Next, you’ll want to scroll down a little bit and locate the following code:

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

Immediately before the above code, you’ll want to place this code:

<?php } /* End of is_comment statement */ ?>

This will filter out all of the trackbacks and pingbacks from your main comments loop. Now we need to create a second comments loop to display the trackbacks and pingbacks.

3 ) Almost immediately below the code from step 2 you should find this code:

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

Immediately before the above code, you’ll want to place this code:

<h3>Trackbacks</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>

You can adjust this code to display how you want to, including using a different header if you have a specific look for your header 3.

Digg This! | Stumble it! | Add to Del.icio.us | | Print This! |

There Are 69 Responses So Far. »

  1. 1 Garry Conn
    Saturday, October 6th, 2007 at 12:11 pm

    Great article man. I found it after my friend Kyle from Kyle’s Cove included a reference to it in his article. Nice work man!

  2. 2 Adam
    Monday, October 8th, 2007 at 10:22 am

    i like the article, but do you know how i can list the number of trackbacks….

    Yours above says 3 Responses…

    Could it say 3 Trackbacks too?

  3. 3 Hack WordPress
    Monday, October 8th, 2007 at 10:44 am

    Adam - Glad you found the post useful!

    With my current setup, the comments loop starts after it grabs the number of comments from the database, causing it to group the comments and trackbacks into a single total.

    Unfortunately, WordPress handles trackbacks/pingbacks and comments in the same way (which is why you have to approve trackbacks like you would a comment). Therefore, it would be extremely difficult to code your comments.php file to pull them separately.

  4. 4 Jenny
    Tuesday, October 16th, 2007 at 4:22 pm

    Oh good I’ve been trying to figure out how to do this. Thanks!

  5. 5 Hack WordPress
    Tuesday, October 16th, 2007 at 5:45 pm

    Jenny - Glad you found it useful!

  6. 6 blogger pl . com
    Thursday, October 18th, 2007 at 7:25 pm

    Adam,
    to count comments and trackbacks separately you should try as follow: immediately after line

    put that code:

    and then simply show right number as number of comments or trackbacks.

  7. 7 blogger pl dot com
    Thursday, October 18th, 2007 at 7:31 pm

    ups, it seems that code I wrote isn’t displayed, than again:

    <?php
    $numTrackBacks = 0;
    $numComments = 0;

    foreach ($comments as $comment) {
    if (get_comment_type() != “comment”) { $numTrackBacks++; }
    else { $numComments++; }
    }
    ?>

  8. 8 engtech
    Sunday, October 21st, 2007 at 10:43 am

    Not quite the same thing, but I have two RSS filters for separating my comments and trackbacks in my RSS feeds in NetVibes — so I can always see the last 5 commenters.

    http://internetducttape.com/2007/09/25/blog-maintenance-tip-netvibes-start-page/

  9. 9 Michael from Pro Blog Design
    Sunday, October 21st, 2007 at 11:56 am

    Heck of a post. ;)

  10. 10 Bradly Fletchall
    Thursday, October 25th, 2007 at 6:51 pm

    This is great stuff. This is by far the esiest to understand Wordpress Theme adjusting I have done since I switched to WordPress a few months ago. I’m going through the other articles looking for other golden nuggets of knowledge I can use on my site.

  11. 11 Hack WordPress
    Friday, October 26th, 2007 at 1:45 pm

    Blogger PL - Thanks for contributing the extra code!

    Engtech - That is definitely useful. Thanks for providing the link.

    Michael - Thanks! Hope you check back for future updates.

    Bradly - Glad you found it useful! I try to discuss various news, tips, tricks, hacks, and WordPress plugins/theme reviews, so if you find that stuff useful feel free to subscribe to my feed!

  12. 12 Mike - Twenty Steps
    Sunday, October 28th, 2007 at 11:09 am

    That’s a great workaround and something I’ve just implemented on my site. Thanks for putting it out there.

  13. 13 Vikram
    Monday, October 29th, 2007 at 7:15 pm

    Hey! It works! This mix up of trackbacks and comments was really pissing me off, but this is a great Tutorial, they each have their own territory now!

    Cheers! Great job!

  14. 14 mk
    Thursday, November 1st, 2007 at 11:51 am

    Duh! First of all, thanks for pointing out that this is a themes issue. :-)

  15. 15 Ben K.
    Friday, November 16th, 2007 at 5:47 pm

    Thanks for this one. The Comments/Trackback issue was really bugging me.

  16. 16 Marc
    Saturday, February 2nd, 2008 at 2:52 pm

    I wanted to separate them but thought it might be too complicated. Thanks for the tip.

  17. 17 jrgd
    Sunday, February 3rd, 2008 at 2:55 pm

    Thanks for all those explanations: I just did it and it’s quite nice to have trackbacks and comments separated now, I really like it! Thanks!

  18. 18 valmg
    Thursday, February 14th, 2008 at 3:34 pm

    Awesome post! Very helpful and looks terrific when implemented. It was easy to put on this blog, I’ll be putting it in place on my others as well. Thanks! Great work!

  19. 19 Adam Kayce
    Wednesday, March 12th, 2008 at 3:55 pm

    Beautiful! This worked exactly as I wanted it to, even taking out the trackback text, and it did it in about five minutes.

    Thanks so much (you saved my hide).

  20. 20 moserw
    Monday, March 17th, 2008 at 11:21 am

    Thanks for sharing this. Will check it out.

    http://www.nela.in/

  21. 21 Mac Tips
    Saturday, April 12th, 2008 at 9:02 am

    Is there a way to set it up so it says a line of text if there isn’t any trackbacks. In a similar way to comments. Otherwise a very good tip. Thanks

  22. 22 Monika
    Saturday, April 19th, 2008 at 7:24 am

    is it correct?
    foreach ($comments as $comment) {
    if (get_comment_type() != “comment”) { $numTrackBacks++; }
    else { $numComments++; }
    }

  23. 23 Kevin
    Saturday, April 19th, 2008 at 3:23 pm

    Definitely a great help! Simple and clean, thanks.

  24. 24 christine
    Thursday, April 24th, 2008 at 2:58 pm

    This is a really great post. Thank you.

    How do you separate Recent Comments from Recent Trackbacks in your sidebar?

    My Recent Comments is now a widget that came with the (Revolution) theme, but it shows the comments and trackbacks all mixed together.

    And even more annoying, it shows trackbacks from internal links between my own articles. Does the code above prevent that? or do you have to somehow exclude it from showing trackbacks where you yourself are the author, or from your own URL, or something like that?

    I’m going to look through your other posts for other good things.
    Thanks again.

  25. 25 Kyle Eslick
    Thursday, April 24th, 2008 at 5:23 pm

    @ Christine - If you are already using the Revolution theme, it should be really easy! You use the Recent Comments plugin provided.

    With that plugin you can sensor out internal pings and set up trackbacks to be separate. You should see a trackback tab.

    I’ll put this on my list of posts to tackle in the near future, so subscribe or check back regularly!

  26. 26 christine
    Friday, April 25th, 2008 at 4:08 am

    Thanks, Kyle.
    I have a Recent Comments widget, but I can’t get in to change anything about it.
    There is no Recent Comments plugin. That one you mentioned sounds excellent. I’ll go look for it, instead of the widget.
    If you know of a good one, could you post a link?
    Thanks again.

  27. 27 christine
    Friday, April 25th, 2008 at 4:17 am

    I think I found it. It’s called Get Recent Comments, right?
    I wonder if it was there at the start and I somehow lost in during the 2.5 upgrade.
    On the plugin in page, here

    http://blog.jodies.de/archiv/2004/11/13/recent-comments/

    it says “Widget support (but not required)”. What does that mean?

  28. 28 Kyle Eslick
    Friday, April 25th, 2008 at 9:01 am

    @ Christine - Yes, that is the correct plugin.

    I don’t use widgets, so I think they are just saying widget use isn’t required.

    I will try to get up a plugin review within the next week for this plugin. Hopefully that will give you the information you need. :D

  29. 29 Slave to the Internet
    Tuesday, May 13th, 2008 at 8:04 am

    Hey, thanks for this little hack! Works a treat and has really cleaned up my comments!!

  30. 30 ovidiu
    Friday, May 16th, 2008 at 1:57 am

    hello there.
    I have done things like this mods, but I would want my trackbacks/pingbacks to look more like they look in my comment section of the admin panel, with all that text around the link to my site, not just the link from where they resulted… but when I tried to use $comment->comment_text it gave me noting back :-(

  31. 31 ovidiu
    Friday, May 16th, 2008 at 1:59 am

    oh and by the way there is a plugin to do this separation: http://www.binarymoon.co.uk/projects/bm-track-ping/

Trackbacks/Pingbacks


Leave A Comment