• Separating Comments and Trackbacks

    One thing I don’t love about WordPress, is its way of including comments, pingbacks, and trackbacks all in one list, and doesn’t provide an easy option to separate them. Separating these gives your comments area a cleaner, more professional feel. Here’s how to do it:

    You’ll need a self-hosted copy of WordPress 2.0 or higher to do this. This functionality was added in WordPress 2.0, so if you are running 1.x, you’ll need to upgrade. You’ll need to edit comments.php (located in your theme folder), so you can either use the Theme Editor (WP-Admin -> Presentation -> Theme Editor), or you can download comments.php (located at /wp-content/themes/yourtheme/comments.php) and edit it in your favorite code editor.

    Now we have to start editing the code! First of all, look for the following code:

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

    Directly below the above code, paste the following:

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

    That code is telling WordPress to get the comment type, and if the comment type is equal to ‘comment’, which is a human comment, display it.

    Next, we’ll need to insert some more code. Look further down the comments.php file, until you see:

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

    Place the following code, just above the endforeach code:

    <?php } else { $trackback = true; } ?>

    Lastly, you’ll need to place the code to display the trackbacks, and also to hide the trackbacks title, if there aren’t any pingbacks or trackbacks yet. Place the code, right before this:

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

    Here’s the code to paste before that:

    <?php if ($trackback == true) { ?>
    <h3>Trackbacks</h3>
    <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 } ?>

    This code is telling WordPress to show all the comments that don’t have the comment type of ‘comment’, to display. This includes pingbacks and trackbacks. Now if you look onto a post page (you might need to reset your cache if you are using WP-Cache or something similar), you’ll notice an area showing comments, and a list of links for the trackbacks and pingbacks.

    If you want to place the trackbacks above the real comments instead of below the comments, place the trackbacks code (previous code snippet) directly after this:

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

    This post was written by Chris Thomson, a young teenage podcaster and blogger.

    Post Revisions:

    Posted February 3, 2008

10 comments

  1. Michael Castilla said:

    Thanks for the post Chris!

    I’ve definitely got to do this for the next WPCandy theme.

    on February 3, 2008 at 9:15 pm Reply

  2. Kyle Eslick said:

    Wow, heck of a post! :mrgreen:

    on February 3, 2008 at 10:35 pm Reply

  3. Chris Thomson said:

    Hey Kyle,

    I actually saw your post similar to this a while back, but the thing I didn’t like about your version is that “Trackbacks/Pingbacks” is shown all the time, even if they’re no trackbacks. With the version above, it hides the “Trackbacks/Pingbacks” title, if there are none. :)

    on February 3, 2008 at 10:44 pm Reply

  4. Jermayn Parker said:

    Thanks!!

    I tend to prefer using code instead of Plugins, ta

    on February 3, 2008 at 10:59 pm Reply

  5. Kyle Eslick said:

    Hey Kyle,

    I actually saw your post similar to this a while back, but the thing I didn’t like about your version is that “Trackbacks/Pingbacks” is shown all the time, even if they’re no trackbacks. With the version above, it hides the “Trackbacks/Pingbacks” title, if there are none. :)

    Good thinking!

    I guess I like to have it there all the time as a subtle reminder/recommendation for readers to send a trackback/pingback to display there! A lot of times if you can be the first or second one on a popular post you can generate some traffic for yourself.

    on February 4, 2008 at 11:27 am Reply

  6. One Winged Angel said:

    Very useful. Thanks! :)

    on February 24, 2008 at 10:12 am Reply

  7. Peter said:

    Post ‘inspired’ by this one ?

    http://hackwordpress.com/how-to-separate-wordpress-comments-and-trackbacks/

    on March 25, 2008 at 2:40 pm Reply

  8. zinni said:

    I don’t know how I never really dug deeper into your blog? I read / downloaded the tip sheets yet never realized the wealth of knowledge this site really has. This is yet another tip that I otherwise would have spent some serious time researching how to do the correct way.

    on April 3, 2008 at 10:46 pm Reply

  9. Sean Grimes said:

    Thanks a bunch! I’m a newbie to PHP, but I figured out that i had to change the following characters in your code:

    < change to

    Easily did this in notepad editior.

    Cool!!

    on October 3, 2008 at 1:52 pm Reply

  10. Sean Grimes said:

    well, the the characters were translated by the html so it did not show in my last post.

    &,lt,; =

    Had to add commas to get it to display here.

    on October 3, 2008 at 1:56 pm Reply


RSS feed for comments on this post.

Leave a comment

Trackbacks on this post

  1. The Status of my Blog365 ‹ The Blog of Chris Thomson

    [...] I wrote my first post at WPCandy as a contributing writer. I’m writing for WPCandy because I love writing about [...]

  2. WordPress Weekly Episode 5 » Jeffro2pt0.com

    [...] Separate Comments And Trackbacks – More and more blog readers/commenters are becoming annoyed with wading through the trackback/pingbacks that seem to clog up the commenting section of so many blogs, this one included. Chris Thomson has written a good article which describes what you need to do in order to separate the two without the need of a plugin. Thanks Chris! [...]

  3. 区分开 Comments 和 Trackbacks

    [...] 翻译自:Separating Comments and Trackbacks,有节选,请注意。 cT=”0″;nc=”#444444″;nBgc=”";nBorder=”#F5E5A9″;tc=”#649B00″;tBgc=”#FFF4D0″;tBorder=”#F5E5A9″;tDigg=”%E6%8E%A8%E8%8D%90″;tDugg=”%E5%B7%B2%E8%8D%90″;defaultItemUrl=”WEB_URL”;defaultFeedUrl =”http://feed.fairyfish.net”; [...]

  4. 区分Comments和Trackbacks | Broken Arrow

    [...] 翻译自:Separating Comments and Trackbacks,有节选,请注意。 [...]

  5. 明年今日

    将留言区分显示Comments和Trackbacks

    WordPress 不好的一个地方就是把 comments,pingbacks,和 trackbacks 一团糟放在一个列表里面,并且没有提供一个把它们区分开的选项。把它们区分开能够给你一个更清晰的留言区,并且能够给读…

  6. 区分开 Comments 和 Trackbacks - Mr.bssn 的个人博客

    [...] 翻译自:Separating Comments and Trackbacks,有节选,请注意。 Filed under: wordpress 性命呼叫转移 » [...]

  7. WordPress Weekly Episode 5 | Jeffro2pt0

    [...] Separate Comments And Trackbacks – More and more blog readers/commenters are becoming annoyed with wading through the trackback/pingbacks that seem to clog up the commenting section of so many blogs, this one included. Chris Thomson has written a good article which describes what you need to do in order to separate the two without the need of a plugin. Thanks Chris! [...]

TrackBack URL