Separate Trackbacks from Comments on Your WordPress Blog

I wonder why WordPress theme designers don’t implement this feature more often. Separating trackbacks from comments is a very simple tweak that will greatly improve the user experience for the people that comment or read comments on your blog. If you keep them together (as I used to until recently…) the readers will see a bunch of links and snippets of code mixed with the real comments, making it difficult to follow the conversation.

So how do you do it? You will just need to open the comments.php file and edit some lines. Michael from Pro Blog Design has a step by step tutorial for this purpose, and he allowed me to adapt it here.

First of all locate the following line inside your comments.php file (which is located under “Presentation” and then “Theme Editor” on your Wordpress control panel).

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

Right after that line, paste the following code:

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

These lines will identify the type of the comment in question, and it will only publish real comments, while filtering trackbacks and pingbacks. Now you will need to close the PHP function, so again look for the following line:

<?php endforeach; /* end for each comment */ ?>
Before that line paste the following code:

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

That is it; you are now filtering out all the trackbacks and pingbacks. Some people consider it better to remove trackbacks altogether since they are often target of spammers. I don’t agree with this solution. Trackbacks are an essential part of the blog world, just improve your spam blocking tools and you should be fine.

The question then becomes: should I place the trackbacks before or after the comments? Both methods can work, depending on what you are trying to accomplish. Placing the trackbacks right after the post (and before the real comments) might encourage people to link to your posts since their trackback links will be displayed prominently. Placing the comments first, on the other hand, will encourage readers to leave more comments and to follow the conversation more closely.

The code to add a “Trackbacks” section is the following:
<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>

Notice that you can customize the title (”Trackbacks,” “Backlinks,” “Blog Reactions” and so on), the header tag (H1, H2, H3 and so on and the list type (ordered or unordered).

Now, if you want to insert the “Trackbacks” section before the real comments you will need to paste that code after the comments header, which looks like this:

<h2 id="comments"><?php comments_number('No Responses', 'One Response', '% Responses' );?> to “<?php the_title(); ?>”</h2>

You might also want to add another header before the real comments to call them out.

Finally, if you want to place the “Trackbacks” section after the real comments just paste the code before this line:

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

Don't want to miss a single tip? Subscribe to our RSS Feed!

46 Responses to “Separate Trackbacks from Comments on Your WordPress Blog”

  1. Justin Dupre on January 8th, 2008 6:15 am

    Thanks for this! I will definitely be looking at my coding and configuring it on my blog!

    Justin Dupre

  2. David Zemens [1955 Design] on January 8th, 2008 6:33 am

    Terrific post! I was literally wondering how to accomplish this when I stumbled on your post. Great advice. I implemented it on my blog within five mintues and it works great!

    Thank you.

  3. Jorge Otero on January 8th, 2008 6:41 am

    Great tip. I think it needs a bit of extra work in order to display the “Trackbacks” header only if there are any trackbacks. I’ll see if I can hack it.

  4. Daniel on January 8th, 2008 6:45 am

    Jorge, I saw someone doing it on another blog also, I will try to find it out.

  5. TheBloggerTips on January 8th, 2008 7:01 am

    Is there any plug-in for this trackbacks feature?

  6. Daniel on January 8th, 2008 7:03 am

    Not that I know, would be easy to code one though, I will check with some coders to see if they are interested.

  7. Vijay on January 8th, 2008 7:11 am

    Thankfully, my theme does it by default. And I must say that it is a very useful feature.. And thanks a bunch for the tut!

  8. Ben on January 8th, 2008 8:16 am

    I wrote about this some time ago and use a slightly different method.

    I am also making a theme that makes use of the same code. I’ve been thinking about making a plugin that does this for you but am not sure how best to work it since people style and lay out their comments in different ways.

  9. Michael from Pro Blog Design on January 8th, 2008 10:03 am

    Thanks for the credit link Daniel. Separating out the trackbacks is one of the easiest ways to improve commenting on a blog. They’re a nusiance otherwise! :)

  10. engtech @ internet duct tape on January 8th, 2008 10:36 am

    I’ve got some yahoo pipes that let you do something similar with the RSS feed.

    This lets me subscribe to comments only.

    http://pipes.yahoo.com/pipes/p.....vMIi1vC6Jw

    http://pipes.yahoo.com/pipes/p.....gDhTouNLYQ

  11. Carla on January 8th, 2008 10:40 am

    thanks for the tip

  12. Fashion By Jenni on January 8th, 2008 12:27 pm

    Is this wordpress.com or wordpress.org or both?

  13. Daniel on January 8th, 2008 1:07 pm

    Both, but I think in Wordpress.com you need to pay to be able to edit the stuff.

  14. David Zemens [1955 Design] on January 8th, 2008 2:40 pm

    In order to limit the Trackbacks header to displaying only when their actually are one or more trackbacks, I modified your code to assign a variable in your original For/Each loop:

    <?php if($comment_type != 'comment') { $is_trackback = "is_trackback"; } ?>

    This code checks to see if ANY of the comments in the loop are
    trackbacks, and assigns the variable as shown.

    Then, in the code that displays the actual trackbacks (which in my case
    is below the comments) this is my code:
    <?php if ($is_trackback == "is_trackback") { ?>
    <h4>Trackbacks to Other Blogs</h4>
    <ul id="trackbacks">
    <?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; ?>
    </ul>
    <?php } ?>

    I wrapped the output code with the IF statement which is ONLY output if
    the $is_trackback variable from the earlier FOR NEXT loop indicates that
    there is at least one trackback. In other words, if there are no
    trackbacks counted in the original loop then this code, and it’s header
    do not display.

    That should do it. It works for me and should work for you, too.

  15. Daniel on January 8th, 2008 3:19 pm

    David, send the code to me via email and I will update it here.

  16. Joel on January 8th, 2008 6:55 pm

    I have been playing with my theme over the past few weeks.

    I found a tutorial about separating the comments and trackbacks. It involves a separate function (split_comments) that I have in my functions.php file. My theme is kind of warped, so I did some major overhauling of it and hopefully made it a little easier to work with.

    I have included Gravatars in the comments, separated the comments and trackbacks, and after reading some of these comments, I added a simple if statement to keep from displaying the Comments header when there are only trackbacks. I think the split_comments function is what allows me to easily run the if statement.

    I just used that code, modified it a bit, and then threw in the following right before the Comments header.


    if ( count( $bm_comments ) > 0 ) {
    ...
    }

  17. Jermayn Parker on January 8th, 2008 7:57 pm

    Good to see the code and thanks for providing that. I look forward to seeing Davids for/ each code to make this even more useful, thanks

  18. pablopabla on January 8th, 2008 8:52 pm

    Yes, I learnt from Michael when he posted his tutorial. My blog looks much better now by having the trackbacks nicely separated from the comments. Definitely neater that way.

  19. Glenda Watson Hyatt on January 8th, 2008 9:20 pm

    Thanks! That wasn’t painful at all. But, to have the trackbacks appear before the comment headings, I had to insert the “Trackbacks” section BEFORE the comments header - instructions say after. Or did I misinterpret something?

  20. CypherHackz on January 8th, 2008 11:59 pm

    That is what I want to implement on all my themes I use. Thanks for the tips.

  21. Inge on January 9th, 2008 3:00 am

    There’s a terrific plugin for this purpose that will exactly do all this for you, but then automatically. Advantages: less work, no extra work when you upgrade Wordpress, and numerous other interesting options regarding how to display your comments.

    http://blog.jodies.de/archiv/2.....-comments/

  22. Thilak on January 9th, 2008 10:48 pm

    Ouch! I got my theme designed by Micheal, but it doesn’t separate comments from trackback. Thanks for the reminder, I’ll do it right away.

  23. Joe on January 10th, 2008 9:13 am

    I just did this on my blog. Awesome. Thanks!

  24. Matt on January 11th, 2008 7:32 am

    Thanks a lot - just added the code to my theme :)
    Tried Davids Zemens Mod… hopefully I did it right ;)

  25. Napster on January 11th, 2008 12:22 pm

    Excelent tutorial guys! Keep it up!

  26. Nick - road2blogging on January 20th, 2008 4:27 pm

    Thanks for the tip, I think I’m going to have to implement this when I get my new design next month.

  27. Free Blog Templates on January 25th, 2008 8:48 am

    @ David Zemens (comment #14)

    Great tip…I implemented it per your code. Just make sure when you add this code in the loop

    it’s above this code

    I had it the other way around and if you don’t have any comments on the post but some trackbacks, the trackbacks won’t show.

    Great article overall..thanks for the tips!

    ~David

  28. photodropper on February 22nd, 2008 9:44 am

    Awesome. Thanks for the tip - I just added this to our site - it really does make the blog look cleaner.

  29. morbEcorilm on February 29th, 2008 3:01 pm

    Hello there.
    Just found your site. Great job!
    I like it much.
    look here http://live.com

  30. Leo F. Swiontek on March 30th, 2008 2:29 am

    Good to see the code and thanks for providing that. I look forward to seeing Davids for/ each code to make this even more useful, thanks

  1. Le ultime notizie più succulente dal mondo dei blog - Edizione del 09 gennaio 2008 | MondoBlog
  2. Wieder voll dabei - Takealook
  3. double density design - » Bookmarking the web - w02/2008
  4. Link-log #3 - 11/01/2008 — Freak Group
  5. WordPress Weekly - Episode 2 » Jeffro2pt0.com
  6. Separer les commentaires des trackbacks dans WordPress ! Ainsi que leur compteur ! | WordPress Francophone
  7. Creating a Better Wordpress Comment Section : Pressing Pixels | A Wordpress Magazine
  8. 4 Enhancements to Improve Your Blog’s Comments Section
  9. This Month In SEO - 1/08 - TheVanBlog
  10. How to Create a Trackback | Dipping into the Blogpond
  11. Why Trackbacks Are Unnecessary » Reader Appreciation Project
  12. WordPress Tweak: Seperate trackbacks from comments
  13. Findability Today » Link Fun Friday 02/29/08 The Leap Edition!
  14. bm trackPing - comments, trackbacks and pingbacks separated » Binary Moon » The home of Ben Gillbanks
  15. Blogosfera: Le Ultime Notizie - Edizione del 09 gennaio 2008 | MondoBlog
  16. WordPress Weekly - Episode 2 | Jeffro2pt0

Got something to say?





Sponsors

Online Invoicing For Freelancers Why I recommend Doreo Hosting More Traffic for Your Blog Premium WordPress Themes Free WordPress Themes Yougler Plugin - Reliable Contact Form

Popular Articles

Recent Articles

Subscribe via E-Mail


Trying to Find a Good Domain?

killerdomainsbook1.jpg