Separating Pingbacks and Trackbacks from Comments in Wordpress

This isn’t something blogs do all that often, but ever since I saw how JohnTP separates his pingbacks and trackbacks from his regular comments I’ve wanted to take the time to figure out just how to do that myself.

From the user’s perspective, separating pingbacks and trackbacks provides a nice, clean look that doesn’t break up the flow of reader comments. As such, I hope every blog seriously considers separating pingbacks and trackbacks from regular comments. If you’re still having doubts I’d like to make the choice easier for you: the steps below show you how to separate comments from pingbacks and trackbacks.

First, you will need to modify the comments loop in your template’s comments.php file and add the following code:

<?php $comment_type = get_comment_type(); ?>

This will make the $comment_type variable available so that you can place an if () statement to determine if the comment is a comment, pingback, or trackback.

Next, you’ll need to add an if () statement inside of your comments loop so that only normal comments are shown. To do this you’ll need to wrap all of the code inside of the comments loop between:

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

and

<?php } ?>

Now that you’re only showing normal comments, you’ll need to make a copy of your comments loop so that you now have two comment loops, one right after the other. The only change you’ll need to make in the second loop is to change:

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

to

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

Now all you have to do is style the second loop to your liking. For my template I stripped out the excerpt text and added the following code to print out an appropriate indicator for pingbacks and trackbacks:

<?php
if ($comment_type == 'pingback') {
    echo "Pingback";
} else if ($comment_type == 'trackback') {
    echo "Trackback";
}
?>

Now that you know how to separate your regular comments from pingbacks and trackbacks what are you waiting on? Go separate them already!

Tags: , , , , ,

If you enjoyed this post, get free updates by email or RSS.

Checkout These Related Posts

  • How To Crush WordPress Trackback Spam
  • WordPress Plugins
  • Show Unread Comments - New Wordpress Plugin
  • Show Unread Comments: Version 1.1
  • Fixing the Tab Index with Brian’s Threaded Comments
  • 62 Comments so far »

    1. HMTKSteve said

      on March 15 2007 @ 10:58 am

      I will have to try this out. I already remove all trackbacks from my recent comments plugin.

    2. Everyday Weekender said

      on March 15 2007 @ 10:20 pm

      Great post, I will try and implement this on my blog once the time is right :)

    3. Ryan J. Parker said

      on March 16 2007 @ 12:10 am

      Let me know how it goes and if the steps above help or not.

      Oh, and I modified my setup: I moved the pingbacks and trackbacks to below the comment box so that when you leave a comment you can see the most recent comment instead of a long list of pingbacks and trackbacks.

    4. HMTKSteve said

      on March 16 2007 @ 7:39 am

      Where should each line of code go i nthe comments.php file? I don’t have the time to fully think this out right now.

    5. Wild Bluff Matt said

      on March 16 2007 @ 8:52 am

      I really need to implement this as everytime I linkback to a previous entry I made from a new entry I get a trackback showing up in my comments. It’s kinda annoying. Do you have yours set to just not show the trackbacks?

    6. Everyday Weekender said

      on March 16 2007 @ 8:12 pm

      I tried to set this up and it’s not working properly.. actually I’m having alot of problems with my comments file.. I can’t get the nested comments to work either.

    7. Ryan J. Parker said

      on March 17 2007 @ 11:08 am

      Steve, you have to put the if() statement right after the beginning of the comments loop (probably a foreach loop), and you just end it at the end of the comments loop.

      Matt, I have it setup to show both Pingbacks and Trackbacks. The if/else that echos Pingback/Trackback above is part of that. :D

      Everyday, make sure you put the if() right after the beginning of the comments loop, as I told Steve above. If you need help let me know!

    8. Jim Westergren said

      on March 20 2007 @ 8:17 pm

      Great!

      I had done this earlier but later forgot how I did it. I have bookmarked this page and will probably link to it when that moment is right.

    9. Ryan J. Parker said

      on March 22 2007 @ 4:21 pm

      Jim, I hope this helps! If you have any questions feel free to ask.

    10. Polli said

      on March 26 2007 @ 8:24 pm

      What a wonderful idea! I am very new to CSS and WordPress and I struggled with this for a couple hours today and must be doing something terribly wrong because it never would work. Would anyone like to help me implement this code in my comments.php? I would be happy to pay for time spent via PayPal or offer a link or both. Email me at polli @ polliwogspond.com and I can email you my comments.php file.

    11. Dawud Miracle said

      on March 31 2007 @ 12:23 pm

      Hi Polli. I may be interested. You can contact me through my website if you’d like to talk about it.

      I’m working on a new version of my site and have implemented this on my new theme.

    12. Ryan J. Parker said

      on March 31 2007 @ 3:23 pm

      If you have any advice feel free to share it here. :)

    13. Nirmal said

      on May 10 2007 @ 8:05 am

      Nice post. This one is really useful. I’ll definitely try this out. :-)

    14. Wayde Christie said

      on May 25 2007 @ 11:15 pm

      Finally! I’ve been wanting to do this for quite some time. Great work!

    15. Jenny said

      on May 26 2007 @ 6:36 pm

      Wow. I’ve been wanting to know how to do this for a while. Now I know and I am gonna work on it right now. :) Thanks muchly!

    16. RT Cunningham said

      on May 28 2007 @ 4:13 pm

      Thank you! I’ve been looking for something like this. I’m pretty proficient in PHP, but I didn’t even think about it until receiving two trackbacks in a row today. A Google search led me here and saved me the headache of looking up the functions.

    17. husayndas said

      on July 30 2007 @ 4:48 am

      I tried it, but it’s not working for me. trackbacks and pingbacks are still in the comments section.

    18. husayndas said

      on July 30 2007 @ 4:51 am

      oops! haha! it is working. i forgot to duplicate it within the loop.

      does anyone know how to have separate comments and trackback counts? so it can be like “5 comments and 1 trackback” or whatever?

    19. Ryan J. Parker said

      on July 30 2007 @ 10:14 am

      husayndas, not sure if this is the optimal solution, but you could simply create a counter, say $comments and $trackbacks and then loop through all of them and if it’s a comment do $comments++; and if it’s a trackback do $trackbacks++;

      This way when you go to show each individual type you can print the appropriate variable.

    20. Reel Racing said

      on August 2 2007 @ 11:45 am

      Hey Ryan. How do I identify the start and finish of my comments loop? I am only good with html and css, php is mostly gibberish to me but I really need to make this modification.

      Thanks,
      Jordan

    21. Ron said

      on September 7 2007 @ 11:08 pm

      THANK YOU SO MUCH FOR THIS INFO!

      I’ve been trying to seperate my pingbacks from my comments for my blog for the longest time, and within 10 minutes reading this tutorial I got it working how I wanted.

      THANKS AGAIN!

    22. Tobi said

      on September 17 2007 @ 3:25 pm

      Hey Guys,

      thanks for your efforts but its a little hard to understand for a complete layman in PHP.

      Can anybody post a complete edited comments.php or c&p the basic-content if this file?

      I think, this modification is only interesting in combination with a counter on the blog start screen: “Posted in … , X comments, X trackbacks, X pingbacks.

      Is it possible that anybody post a useable file/link to reproduce the above mentioned things?

      Thanks so lot

      T!

    23. Whatever-ishere said

      on November 21 2007 @ 1:35 pm

      thanks for the GREAT post! Very useful…

    24. Paszternak said

      on November 27 2007 @ 12:37 pm

      Thanks for the tutorial, worked great! :)
      One tiny question: is there a way to count comments and trackbacks/pingbacks separately?
      Thx,

      Adam

    25. naisioxerloro said

      on November 28 2007 @ 10:37 pm

      Hi.
      Good design, who make it?

    26. Christopher Ross said

      on October 21 2008 @ 11:30 pm

      Thanks Ryan, this is a great little piece of code. I’ve edited a bit for my own purposes but really appreciate giving me a place to start. Chris

    27. James Wee said

      on March 6 2009 @ 7:20 am

      Wow, perfect. For those who faces problem. I followed both this and another post …
      http://www.dailyblogtips.com/separate-trackbacks-from-comments-on-your-wordpress-blog/

      Thanks Ryan

    28. RaiulBaztepo said

      on March 28 2009 @ 6:31 pm

      Hello!
      Very Interesting post! Thank you for such interesting resource!
      PS: Sorry for my bad english, I’v just started to learn this language ;)
      See you!
      Your, Raiul Baztepo

    29. sooran said

      on September 15 2009 @ 6:36 am

      tancks.
      This Post Helped me
      Good Time

    30. Ezuca said

      on October 2 2009 @ 7:22 pm

      Truly awesome. I never knew it was only that easy. Thanks!

    31. Lisa said

      on October 13 2009 @ 6:45 pm

      Okay, still don’t understand this trackbacks thing. I don’t know html, but it is necessary to have this? My site is informational and offers products.

    32. Coder29 said

      on October 22 2009 @ 12:25 pm

      One report showed that youths with an average grade of D or below were more than four times as likely to have used marijuana in the past year as youths with an average grade of A. ,

    33. Barbara78 said

      on October 23 2009 @ 9:38 am

      They are outnumbered many times by the fresh French troops they will face in battle the next day. ,

    34. blackharvardwomen blog said

      on November 5 2009 @ 11:05 pm

      i am newbie at php programming but i will try your instructions to separate pingbacks and trackback to my blog comments

    35. Online Car Insurance >> http://onlinecarinsuranceclaims.com/ said

      on November 23 2009 @ 5:29 pm

      [... - http://www.ryanjparker.net is other wonderful website of advice. Car insurance claims [… -

    36. eveptacle said

      on November 24 2009 @ 10:31 am

      Great Article , I considered it grand

      I look forward to more great postings like this one. Does Your Blog have a newsletter I can subscribe to for new postings?

    37. Used Tires said

      on November 27 2009 @ 3:25 am

      Pretty easy to follow instructions my friend, should be easy to implement =D

      Till then,

      Jean

    38. silicone breast implants said

      on November 27 2009 @ 11:38 am

      I know there’s suppose to be a import tool on blogger but it isn’t there. If the tool was from blogger to blogger then that’s fine too..

    39. bunatut said

      on December 19 2009 @ 10:26 am

      Excellent Resorces on Property Information

    40. Guildford said

      on December 21 2009 @ 2:34 am

      i will give this a try. Thanks for the small tutorial of how to do it.

    41. Designer Resources said

      on December 22 2009 @ 9:25 pm

      nice tutorial thanks for.

    42. vektörel said

      on December 29 2009 @ 3:14 pm

      Where should each line of code go i nthe comments.php file? I don’t have the time to fully think this out right now.

    43. tattoo kits said

      on January 3 2010 @ 1:59 pm

      Good post _ i will have to try this out!

    44. Burc Yorumu said

      on January 7 2010 @ 3:10 am

      Great Article, Thanks My Friend

    45. Phil Luther said

      on January 9 2010 @ 11:35 am

      Great post, and great info. I’ve never really understood why WP treats comments and pingbacks as the same things … but you’ve got a 5-minute fix for it.

      I especially dig the conditional inside the comment loop, so you can do almost freakin’ anything on the display-side.

      Well played.

    46. Boxing Bags said

      on January 9 2010 @ 6:08 pm

      good post - thanks for the heads up on this!

    47. online stock trading advice said

      on January 10 2010 @ 8:24 pm

      Your site was extremely interesting, especially since I was searching for thoughts on this subject last Thursday.

      I’m Out! :)

    48. Xbox 360 cheat said

      on January 13 2010 @ 4:28 pm

      I know there’s suppose to be a import tool on blogger but it isn’t there. If the tool was from blogger to blogger then that’s fine too..

    49. oyun said

      on January 26 2010 @ 11:33 am

      I identify the start and finish of my comments loop? I am only good with html and css, php is mostly gibberish to me but I really need to make this modification.

    50. Comment RSS · TrackBack URI

    Leave a comment

    Name: (Required)

    eMail: (Required)

    Website:

    Comment:

    Pingbacks/Trackbacks

    1. Pingback on March 30 2007 @ 6:23 pm

    2. Pingback on May 4 2007 @ 1:45 pm

    3. Pingback on May 25 2007 @ 9:16 pm

    4. Pingback on June 1 2007 @ 11:13 am

    5. Pingback on July 24 2007 @ 7:23 am

    6. Pingback on August 11 2007 @ 9:12 am

    7. Pingback on August 19 2007 @ 2:00 am

    8. Pingback on January 25 2008 @ 6:38 pm

    9. Pingback on February 6 2008 @ 2:30 am

    10. Pingback on March 8 2008 @ 6:30 pm

    11. Pingback on March 9 2008 @ 3:01 am

    12. Pingback on February 9 2009 @ 11:06 am

    13. Pingback on October 5 2009 @ 9:06 am