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.

Tags: , , ,
46 replies
« Older Comments
  1. Sell Skype Accs
    Sell Skype Accs says:

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

  2. TeMc
    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()) ?

  3. sevi?me
    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

  4. suraj
    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?

  5. zune music download
    zune music download says:

    You have certainly antecedently been exceptionally strenuous publishing pointing up all of this good weblog, Completely quite interesting to be able to read. Can’t time to wait to find out everything you articles about in the up coming last seven days. New for your huge positive aspects, prefer to I do not really nurturing such a internet site , and after that intend this guidance, too since the great evaluations some other rather folks wrote, ought to aid loved ones resolve in the case when it is some of the ripe choice for you personally . May be the idealfact.

Trackbacks & Pingbacks

  1. […] Explanation on how to separate comments from trackbacks. Visit Article. […]

  2. […] author also mentioned on a another informative post on how to separate comments and trackbacks at Kriesi.at.Most Commented PostsMarch 9, 2009 — SEO: Google Search RankingJanuary 8, 2009 — SEO, Page Rank, […]

« Older Comments

Comments are closed.