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
  • 36 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. 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