Jamie asked: “How can I display comments and trackbacks separately?”

Jamie, a Cats Who Code regular reader who maintains a technology related blog, asked me how I managed to separate comments and trackbacks on Cats Who Code.
Let’s see how the get_comment_type() function make it so easy.

Let's face it: When you're reading comments on a blog post, trackbacks are annoying. It's way better to display it separately from comments.

Open and edit the comments.php file from your theme. Find the comment loop:

foreach ($comments as $comment) : ?>
    // Comments are displayed here
endforeach;

Replace it with the following:

<ul class="commentlist">
    <?php //Displays comments only
	foreach ($comments as $comment) : ?>
       	<?php $comment_type = get_comment_type(); ?>
       	<?php if($comment_type == 'comment') { ?>
	    <li>//Comment code goes here</li>
	<?php }
    endforeach;
</ul>

<ul>
    <?php //Displays trackbacks only
	foreach ($comments as $comment) : ?>
       	<?php $comment_type = get_comment_type(); ?>
       	<?php if($comment_type != 'comment') { ?>
	    <li><?php comment_author_link() ?></li>
	<?php }
    endforeach;
</ul>

As you probably noticied, we used the get_comment_type() hook, which return a string containing the comment type.

Related Posts

No related posts.

19 Responses

Oct 12 2008 07:47

Thanx! Very useful and I love the new site by the way. I added you to my twitter. I’m ibdreamy there. Hope you add me back. Take care. I’m gonna link back to this post so you should see a trackback soon. ;)

Oct 12 2008 09:34

Glad you enjoyed this recipe! I added you on Twitter as well. Talk to you soon!

Apr 10 2009 04:39

Just thought I would point out that this code is a little inefficient as it requires going through the comments twice. A better solution would be to save the output for comments and trackbacks to variables and output them at the end. To avoid the markup getting a little unruly, you could use output buffering.

Jul 23 2009 20:46

Thanks for the tip! but I agree with Michael Mior though.

Oct 27 2009 21:58

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 (ie. no trackbacks)

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

Oct 27 2009 22:28

I fixed it myself !

If you want to make sure any surrounding elements do not show up, here’s a little hack you can do.

Within the first (comments only) foreach, add this:


if($comment_type != 'comment') { $has_trackbacks = "yes"; }

This little line will set $has_trackbacks to “yes” if there are any.

After that, in your second little loop, the foreach for trackbacks only, wrap that entire part from [ul] to [/ul] in an if-statement

[?php if ($has_trackbacks == "yes") : ?]
[h3]Trackbacks[/h3]
[ul]
// trackback stuff
[/ul]
[?php endif; ?]

Go to this pastebin for the final complete code : http://wprecipes.pastebin.com/f688d2016

PS:
- Works in WP 2.8.4
- Replace the [ and ] with the greater-then and less-then arrow-symbols ( > )
PS: JbJ: You forgot to end ?] after the two endforeach;s

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required