Kriesi.at – new media design

WordPress: how to separate comments and trackbacks

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.

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

Trackbacks

  1. С сегодняшнего дня появилась возможность добавлять свои ссылки на главную страницу www.codeart.ru
  2. Discover The Best Of The Web In May 2008 - Opensource, Free and Useful Online Resources for Designers and Developers
  3. Websites you shouldn’t have missed in May 2008
  4. Best Design Articles from May 2008
  5. lillbra » Blog Archive » links for 2008-06-02
  6. Best of May 08 | inspiredology.com
  7. WP’de yorumlarla geri izlemeleri ayırmak » opereysin.com - Seviyeli, kaliteli…
  8. links for 2008-06-05 « RabiFoot at wordpress.com
  9. socialcmsbuzz.com
  10. [WP-Design]如何分開迴響與引用? » Meet See
  11. DON’T MISS: The Best Design Articles from May 2008 | Dalton Trent's Blog
  12. DON’T MEASURE: The Best Design Articles from May 2008 | Dalton Trent's Blog
  13. Websites you shouldn’t have missed in May 2008 | Noupe
  14. Discover The Best Of The Web In May 2008 « the gypsy
  15. Comments on Wordpress Pages | s.pangonilo.com | Ver Pangonilo : Filipino Engineer
  16. Developer's Island - Software Development & Design -How to separate comments and trackbacks « simra

Leave a Reply