WordPress: how to separate comments and trackbacks

Tutorials, Wordpress Tutorial May 29, 2008 45 Comments

Today I read an interesting article on NETTUTS which claims to unravel the Secrets of WordPress & Comments.php File. This is actually pretty true, the author did a good job at explaining the different functions, comment loops, and form elements.

The one thing I really missed was an explanation on how to separate comments from trackbacks. Discussing on a blog with tons of trackback posts between the ongoing discussion is really annoying.

So here is my little addition to the NETTUTS tuorial:

How to separate comments from trackbacks in WordPress

First thing we need is a working peace of basic comment code, I’ll take the one from NETTUTS: (if you need any explanation on this piece of code, just read the aforementioned article)

<?php if($comments) : ?>
<ol>
    <?php foreach($comments as $comment) : ?>
        <li id="comment-<?php comment_ID(); ?>">
            <?php if ($comment->comment_approved == "0") : ?>
            <p>Your comment is awaiting approval</p>
        <?php endif; ?>
        <?php comment_text(); ?>
            <cite><?php comment_type(); ?> by <?php comment_author_link(); ?> on <?php comment_date(); ?> at <?php comment_time(); ?></cite>
        </li>
    <?php endforeach; ?>
</ol>
<?php else : ?>
    <p>No comments yet</p>
<?php endif; ?>

Now we will use the function get_comment_type(); to check which kind of comment this is. The function returns one of three possible values: pingback, trackback or a comment.
Our first target is to display only the comments. This is easily accomplished with an additional if statement:

<?php if($comments) : ?>
<ol>
    <?php foreach($comments as $comment) : ?>
       <?php if(get_comment_type() == "comment") : ?>
            <li id="comment-<?php comment_ID(); ?>">
                <?php if ($comment->comment_approved == "0") : ?>
                <p>Your comment is awaiting approval</p>
            <?php endif; ?>
            <?php comment_text(); ?>
                <cite><?php comment_type(); ?> by <?php comment_author_link(); ?> on <?php comment_date(); ?> at <?php comment_time(); ?></cite>
            </li>
        <?php endif; ?>
    <?php endforeach; ?>
</ol>
<?php else : ?>
    <p>No comments yet</p>
<?php endif; ?>

If you don’t want to display trackbacks you are done now. In case you want to show them, you have to call the comment loop a second time, this time only displaying the comment, if get_comment_type() returns pingback or trackback.

<?php if($comments) : ?>
<ol>
    <?php foreach($comments as $comment) : ?>
        <?php if((get_comment_type() == "pingback") || (get_comment_type() == "trackback")) : ?>
            <li id="comment-<?php comment_ID(); ?>">
                <?php comment_author_link() ?></li>
            </li>
        <?php endif; ?>
    <?php endforeach; ?>
</ol>
<?php endif; ?>

Thats it, not that hard to master and a big improvement in readability for your ongoing discussions.

45 Responses to “WordPress: how to separate comments and trackbacks”

  1. Reply Tom says:

    I agree, keeping separate trackbacks and comments is definitely useful.

  2. Reply subcorpus says:

    nice …
    thanks for sharing …

  3. Reply t.vu says:

    hi

    would you please post a tutorial regarding how to create a comment box in the website for us, please (?) .. i really like your works .. and thank you so much for sharing w/ us ..

    t.

  4. Reply Kriesi says:

    what do you mean by “comment box”?

  5. Reply toothsie says:

    thank you

  6. Reply tu says:

    hi Kriesi,

    it is the box that we leave message on the website (like i’m doing now) .. i searched the information online but didn’t have much luck .. if you have time, would you please teach me how to create the box for people to leave comments or if would you please refer me to any link that teach how to create the comment box, please .. by the way, i’m the new learner too .. thank you so much for replying ..

    t.

  7. Reply Лика К says:

    Круто! А отуда такая информация , если не секрет.;)?

  8. Reply StumbleUpon says:

    Your post makes one think! Great article. Thanks for allowing me to comment!

  9. Reply Hasseappozy says:

    Great blog. I like layout!!!!

  10. Reply Brian says:

    I really like the work you’ve done with AJAX as well as your tutorials. This specific tutorial can be found in various forms on the web. I wouldn’t mind seeing something new, such as an AJAX tabbed version to switch between the comments & trackbacks.

  11. Reply Vishal says:

    I’ve noticed that when there is no Trackback on a blog entry, we see an invalid markup due to the empty tag…is there a way around this??

  12. Reply Vishal says:

    no matter it was my fault, I had the ol tag outside the if statement….lol

  13. Reply Social Bookmarking says:

    I enjoyed reading your work! GREAT post! I looked around for this… but I found you! :) Anyway, would you mind if I threw up a backlink from my site at whiterabbitcult.com to your site?

  14. Reply ChabrellIgaN says:

    Zdraste! Vot takoi vot u vas horoshiy sait. Spasibki.

  15. Reply How to Get Six Pack Fast says:

    I read your blog for a long time and must tell you that your posts are always valuable to readers.

  16. Reply Sell Skype Accs says:

    ??????! ?????? ???????? ????????? ????????? ? ?????????
    ????? ??? ??????????? ? ???? ???????! ?????? ??? ???? ? ???????? ??????! ?????? ?????? ???????? 50% ??????????! ?? ????? ???????!!

  17. Reply ???? says:

    So nice guestbook. Thnx…

  18. Reply Matrix says:

    So nice portal. Thnx…

  19. Reply Alex says:

    So amazing gb. Thnx…

  20. Reply TeMc says:

    How to not display the second list (ol or ul) if there are no pingbacks ?

    I’m using a heading (h3 Trackbacks), like you do, but I don’t want that h3 to show when there are only comments.

    I tried to do an endif; after the comments, and then another if $comments before the trackbacks and it’s heading, but that if returns true (obviously), becuase the comment_type if-statment is within the foreach.

    So how can I do if (has_trackbacks()) ?

  21. Reply sikis izle says:

    So nice portal. Thnx…

  22. Reply ?????? ???????? ?????????? says:

    ??????! ????? ???? ???? ????? ?? ???????, ? ?????? ?????? ????? 24\7\365 :) ? ????????????, ?????? ?? ?????????? ??? ??? ? ???? ?? ????????
    ???????, ??????? ???????? :)

  23. Reply Sena kendali says:

    Very nice, thank’s

  24. Reply sevi?me says:

    ki?ilerin, gruplar?n, olaylar?n, konular?n nas?l aktar?ld???yla ilgili haz?rlad???m “Media Representations” isimli bir ?ngilizce sunumu

  25. Reply suraj says:

    Hi, I am using you newscast wordpress theme and want to separate comments and trackback, i donno more about these languages can u please help me?

  26. Reply black celebs says:

    Sign: wdpad Hello!!! ptpsp and 2023cwcfytggsw and 4563 : Sorry, what did you mean?? A??

Leave a Reply