Separating Pings/Trackbacks from Comments in WordPress 2.7
With the coming release of WordPress 2.7 we now have an easy way to separate out your pings and trackbacks from your comments.
I will be using the default theme WordPress comes with to show you have to do this. Below is the important code you should focus on:
- <?php if ( have_comments() ) : ?>
- <h3 id="comments">
- <?php comments_number('No Responses', 'One Response', '% Responses' );?> to “<?php the_title(); ?>”
- </h3>
- <ol class="commentlist">
- <?php wp_list_comments(); ?>
- </ol>
- <div class="navigation">
- <div class="alignleft"><?php previous_comments_link() ?></div>
- <div class="alignright"><?php next_comments_link() ?></div>
- </div>
- <?php else : // this is displayed if there are no comments so far ?>
- <?php if ('open' == $post->comment_status) : ?>
- <!-- If comments are open, but there are no comments. -->
- <?php else : // comments are closed ?>
- <!-- If comments are closed. -->
- <p class="nocomments">Comments are closed.</p>
- <?php endif; ?>
- <?php endif; ?>
With WordPress 2.7, you would edit the wp_list_comments(); function to look like wp_list_comments(‘type=comment’); and just after line 8 (‹/ol›) put:
- <h3 id="pings">Trackbacks/Pingbacks</h3>
- <ol class="pinglist">
- <?php wp_list_comments('type=pings'); ?>
- </ol>
That bit of code will echo out all the pings, so your final code should look like:
- <?php if ( have_comments() ) : ?>
- <h3 id="comments">
- <?php comments_number('No Responses', 'One Response', '% Responses' );?> to “<?php the_title(); ?>”
- </h3>
- <ol class="commentlist">
- <?php wp_list_comments('type=comment'); ?>
- </ol>
- <h3 id="pings">Trackbacks/Pingbacks</h3>
- <ol class="pinglist">
- <?php wp_list_comments('type=pings'); ?>
- </ol>
- <div class="navigation">
- <div class="alignleft"><?php previous_comments_link() ?></div>
- <div class="alignright"><?php next_comments_link() ?></div>
- </div>
- <?php else : // this is displayed if there are no comments so far ?>
- <?php if ('open' == $post->comment_status) : ?>
- <!-- If comments are open, but there are no comments. -->
- <?php else : // comments are closed ?>
- <!-- If comments are closed. -->
- <p class="nocomments">Comments are closed.</p>
- <?php endif; ?>
- <?php endif; ?>
Ah, but there is a problem, what if you don’t have any comments but you have pings/trackback or visa-versa? You should now be able to wrap each H3+OL combiniation in code like:
- <?php if ( ! empty($comments_by_type['comment']) ) : ?>
- <h3 id="comments">
- <?php comments_number('No Responses', 'One Response', '% Responses' );?> to “<?php the_title(); ?>”
- </h3>
- <ol class="commentlist">
- <?php wp_list_comments('type=comment'); ?>
- </ol>
- <?php endif; ?>
- <?php if ( ! empty($comments_by_type['pings']) ) : ?>
- <h3 id="pings">Pingbacks/Trackbacks</h3>
- <ol class="pinglist">
- <?php wp_list_comments('type=pings'); ?>
- </ol>
- <?php endif; ?>
And that’s it, your Pingback and Comments are now separate. Have a look at the final code below and let me know if I missed anything or if you have any questions.
- <?php if ( have_comments() ) : ?>
- <?php if ( ! empty($comments_by_type['comment']) ) : ?>
- <h3 id="comments"><?php comments_number('No Responses', 'One Response', '% Responses' );?> to “<?php the_title(); ?>”</h3>
- <ol class="commentlist">
- <?php wp_list_comments('type=comment'); ?>
- </ol>
- <?php endif; ?>
- <?php if ( ! empty($comments_by_type['pings']) ) : ?>
- <h3 id="pings">Pingbacks/Trackbacks</h3>
- <ol class="pinglist">
- <?php wp_list_comments('type=pings'); ?>
- </ol>
- <?php endif; ?>
- <div class="navigation">
- <div class="alignleft"><?php previous_comments_link() ?></div>
- <div class="alignright"><?php next_comments_link() ?></div>
- </div>
- <?php else : // this is displayed if there are no comments so far ?>
- <?php if ('open' == $post->comment_status) : ?>
- <!-- If comments are open, but there are no comments. -->
- <?php else : // comments are closed ?>
- <!-- If comments are closed. -->
- <p class="nocomments">Comments are closed.</p>
- <?php endif; ?>
- <?php endif; ?>
Update: In regards to Chris’ question, there is a way to separate out your comments count; after some research you can use the get_comments_number filter, you just need to create function and put it in your functions.php file.
- add_filter('get_comments_number', 'get_new_comment_count', 0);
- function get_new_comment_count( $count ) {
- global $wp_query;
- return count($wp_query->comments_by_type['comment']);
- }
If you don’t already have a functions.php file in your theme, just add one.



1. Chris Coyier