Skip to content Skip to tags Skip to twitter news Skip to blog roll Skip to categories Skip to archives Skip to recent posts

December 10, 2008

5 Comments

Separating Pings/Trackbacks from Comments in WordPress 2.7

by in Tips,Wordpress on December 10, 2008 @ 1:00 pm

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:

  1. <?php if ( have_comments() ) : ?>
  2.     <h3 id="comments">
  3.         <?php comments_number('No Responses', 'One Response', '% Responses' );?> to &#8220;<?php the_title(); ?>&#8221;
  4.     </h3>
  5.    
  6.     <ol class="commentlist">
  7.         <?php wp_list_comments(); ?>
  8.     </ol>
  9.     <div class="navigation">
  10.         <div class="alignleft"><?php previous_comments_link() ?></div>
  11.         <div class="alignright"><?php next_comments_link() ?></div>
  12.     </div>
  13.     <?php else : // this is displayed if there are no comments so far ?>
  14.    
  15.     <?php if ('open' == $post->comment_status) : ?>
  16.     <!-- If comments are open, but there are no comments. -->
  17.    
  18.     <?php else : // comments are closed ?>
  19.         <!-- If comments are closed. -->
  20.         <p class="nocomments">Comments are closed.</p>
  21.     <?php endif; ?>
  22. <?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:

  1. <h3 id="pings">Trackbacks/Pingbacks</h3>
  2.  
  3. <ol class="pinglist">
  4.      <?php wp_list_comments('type=pings'); ?>
  5. </ol>

That bit of code will echo out all the pings, so your final code should look like:

  1. <?php if ( have_comments() ) : ?>
  2.     <h3 id="comments">
  3.         <?php comments_number('No Responses', 'One Response', '% Responses' );?> to &#8220;<?php the_title(); ?>&#8221;
  4.     </h3>
  5.    
  6.     <ol class="commentlist">
  7.         <?php wp_list_comments('type=comment'); ?>
  8.     </ol>
  9.     <h3 id="pings">Trackbacks/Pingbacks</h3>
  10.    
  11.     <ol class="pinglist">
  12.         <?php wp_list_comments('type=pings'); ?>
  13.     </ol>
  14.  
  15.     <div class="navigation">
  16.         <div class="alignleft"><?php previous_comments_link() ?></div>
  17.         <div class="alignright"><?php next_comments_link() ?></div>
  18.     </div>
  19.     <?php else : // this is displayed if there are no comments so far ?>
  20.    
  21.     <?php if ('open' == $post->comment_status) : ?>
  22.     <!-- If comments are open, but there are no comments. -->
  23.    
  24.     <?php else : // comments are closed ?>
  25.         <!-- If comments are closed. -->
  26.         <p class="nocomments">Comments are closed.</p>
  27.     <?php endif; ?>
  28. <?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:

  1. <?php if ( ! empty($comments_by_type['comment']) ) : ?>
  2.     <h3 id="comments">
  3.         <?php comments_number('No Responses', 'One Response', '% Responses' );?> to &#8220;<?php the_title(); ?>&#8221;
  4.     </h3>
  5.    
  6.     <ol class="commentlist">
  7.         <?php wp_list_comments('type=comment'); ?>
  8.     </ol>
  9. <?php endif; ?>
  10. <?php if ( ! empty($comments_by_type['pings']) ) : ?>
  11.     <h3 id="pings">Pingbacks/Trackbacks</h3>
  12.    
  13.     <ol class="pinglist">
  14.         <?php wp_list_comments('type=pings'); ?>
  15.     </ol>
  16. <?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.

  1. <?php if ( have_comments() ) : ?>
  2.  
  3.     <?php if ( ! empty($comments_by_type['comment']) ) : ?>
  4.         <h3 id="comments"><?php comments_number('No Responses', 'One Response', '% Responses' );?> to &#8220;<?php the_title(); ?>&#8221;</h3>
  5.        
  6.         <ol class="commentlist">
  7.             <?php wp_list_comments('type=comment'); ?>
  8.         </ol>
  9.     <?php endif; ?>
  10.    
  11.        
  12.     <?php if ( ! empty($comments_by_type['pings']) ) : ?>
  13.         <h3 id="pings">Pingbacks/Trackbacks</h3>
  14.        
  15.         <ol class="pinglist">
  16.             <?php wp_list_comments('type=pings'); ?>
  17.         </ol>
  18.     <?php endif; ?>
  19.  
  20.  
  21.     <div class="navigation">
  22.         <div class="alignleft"><?php previous_comments_link() ?></div>
  23.         <div class="alignright"><?php next_comments_link() ?></div>
  24.     </div>
  25.     <?php else : // this is displayed if there are no comments so far ?>
  26.    
  27.     <?php if ('open' == $post->comment_status) : ?>
  28.     <!-- If comments are open, but there are no comments. -->
  29.    
  30.     <?php else : // comments are closed ?>
  31.         <!-- If comments are closed. -->
  32.         <p class="nocomments">Comments are closed.</p>
  33.     <?php endif; ?>
  34. <?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.

  1. add_filter('get_comments_number', 'get_new_comment_count', 0);
  2. function get_new_comment_count( $count ) {
  3.     global $wp_query;
  4.     return count($wp_query->comments_by_type['comment']);
  5. }

If you don’t already have a functions.php file in your theme, just add one.

back to beginning of this post back to skip to links

If you liked this article why don't you share it:

Stumble it delicious Digg it Reddit it DZone it Bump it Mixx it! Buzz up! E-mail

5 Responses to “Separating Pings/Trackbacks from Comments in WordPress 2.7”

Nice. Just updgraded my WordPress to 2.7 and got my threaded comments going. Just using the type=”comment” thing to just show comments only. I’ve never been big into the pingback/trackback thing. It’s so WordPress-specific and 75% of the ones I get are in a different language or clearly spammy.

What would be nice is if that comment number would only show the total number of real comments, not comments + trackbacks.

@chris two people just emailed me the same question, I think there is an easy way, give me a few hours to look at the code I have and will post an update.

@Chris, posted a filter, give that a go and let me know your thoughts.

Hello Benjamin! I have one question for you. I would like to know how to make that the trackbacks.. have a different css,, other than comments.tx.

@yulio sorry for the delayed response, I am not at my computer but can answer your question better when I get in front of my code.


Learn from my mistakes, I got burnt by the flame, you don't have to.


RSS Feed Link My Hosting of Choice

69 queries. 0.697 seconds. Powered by WordPress visitor stats