Separating Pings from Comments in WordPress 2.7

WordPress 2.7 has introduced many new features surrounding comments. Of these is AJAX commenting and threaded comments. To take advantage of the later, you must use a function wp_list_comments instead of the old way of looping through the comments array with a foreach. Weblog Tools Collection has a good how to on the old way that can be found here.

I wanted to get this hashed out before 2.7 goes live so that theme designers and anyone else can implement this in time for the release.

I’ll be referencing the default theme from 2.7 in this how to. If you are interested in adding the new commenting features to your current pre 2.7 theme see this how to by Otto.

wp_list_comments is not documented yet on the WordPress codex. But some feature that are worth mentioning are the ability to specify the comment type to display and a callback so that you can decide how to structure the output.

Let us start by taking a look at the new comments “loop”:

<?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; ?>

As you can see it is much simpler than the old comments “loop”. The majority of everything that is happening is now done via the function wp_list_comments.

To remove pings (pingbacks and trackbacks) we only need to make a few small changes. First open up your themes single.php:

Find the following code:

<?php comments_template(); ?>

And change it to:

<?php comments_template('', true); ?>

The above change tells comments_template to create a global array $comments_by_type that we will use later on.

First open up comments.php.

Look for the following code:

<?php if ( have_comments() ) : ?>

Directly below this add:

<?php if ( ! empty($comments_by_type['comment']) ) : ?>

Change this:

<?php wp_list_comments(); ?>

To this:

<?php wp_list_comments('type=comment'); ?>

Directly below the wp_list_comments function we modified is:

</ol>

Directly below this add:

<?php endif; ?>

The if statement prevents the comments heading and ol tags from displaying if you only have trackbacks and pingbacks on this post.

Much easier so far, right?

To display the pings we need to insert the following code beneath the endif we just added:

<?php if ( ! empty($comments_by_type['pings']) ) : ?>
<h3 id="pings">Trackbacks/Pingbacks</h3>

<ol class="commentlist">
<?php wp_list_comments('type=pings'); ?>
</ol>
<?php endif; ?>

The comments “loop” should now look like this:

<?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">Trackbacks/Pingbacks</h3>

	<ol class="commentlist">
	<?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; ?>

Now the pings are displayed below the comments. The above code will show the pings in full comment boxes. I personally like a simple ordered list with a link and title of the ping. To achieve this without a foreach (Thanks Ryan Boren for the tip!)

Open your themes functions.php file and create a callback function for wp_list_comments. The following code should be inserted:

<?php
function list_pings($comment, $args, $depth) {
       $GLOBALS['comment'] = $comment;
?>
        <li id="comment-<?php comment_ID(); ?>"><?php comment_author_link(); ?>
<?php } ?>

Replace this:

<ol class="commentlist">
<?php wp_list_comments('type=pings'); ?>

With this:

<ol class="pinglist">
<?php wp_list_comments('type=pings&callback=list_pings'); ?>

If your theme doesn’t have a functions.php just create it and include the above code.

In this case our full comment “loop” should now look like:

<?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">Trackbacks/Pingbacks</h3>

	<ol class="pinglist">
	<?php wp_list_comments('type=pings&callback=list_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; ?>

One last (optional) task is to modify the comment counts to only reflect the number of comments minus pings.

Open your themes functions.php and add the following code:

<?php
add_filter('get_comments_number', 'comment_count', 0);
function comment_count( $count ) {
	if ( ! is_admin() ) {
		global $id;
		$comments_by_type = &separate_comments(get_comments('status=approve&post_id=' . $id));
		return count($comments_by_type['comment']);
	} else {
		return $count;
	}
}
?>

Again if your theme doesn’t have a functions.php just create it and include the above code.

There you have it. If you have any questions let me know.

About Matt

IF YOU REALLY want to hear about it, the first thing you'll probably want to know is where I was born, and what my lousy childhood was like, and how my parents were occupied and all before they had me, and all that David Copperfield kind of crap, but I don't feel like going into it, if you want to know the truth.

In any case I am employed as a Senior Linux Systems Engineer at Rackspace in San Antonio, TX.

I spent the majority of my college years as an Astrophysics major specializing in black holes. When I got to my senior year and was studying 40 hours a week to stay on track I decided to bail and pursue an easier career.

I spend the majority of my time now contributing to WordPress, developing WordPress plugins, helping out in the WordPress IRC Channel, playing football (not American!), practicing Krav Maga, and last but not least, spending time with my Wife and Son.

This entry was posted in HowTo, PHP, Plugins, WordPress. Bookmark the permalink.

141 Responses to Separating Pings from Comments in WordPress 2.7

  1. ChaosKaizer says:

    good write up, thanks.

  2. Pingback: Get Your Theme Ready for 2.7, Part 2 « planetOzh

    • GaMerZ says:

      I have not took a look at 2.7 codes yet, is $comments_by_type a global variable?

      <?php $pings = get_comments_by_type('type=pings'); ?>
      <?php foreach ($comments_by_type['pings'] as $comment) : ?>

      Note the $pings instead of $comments_by_type['pings'];

  3. Nathan Rice says:

    Absolutely fantastic tutorial! I was wondering if there was going to be a function of the new comments API that would allow this to happen more easily.

    I just tweeted this :-)

  4. Matt says:

    I just updated the how to with a tip from Ryan Boren about using a callback in wp_list_comments to build the output the way that you want it.

  5. Pingback: sivel.net has a howto on using the new c … « WordPress Development Updates

  6. Rahul Bansal says:

    Nice one. :-)
    I hope some senior coder will create dedicated page for “wp_list_comments()” on wordpress.org Codex. Because Codex is missing a lot of docs. :-(

  7. Matt says:

    Just updated the how to. The comments_template function now accepts an argument for separating comments.

    <?php comments_template('', true); ?>

    When this change is made in single.php the array $comments_by_type will be defined and will not have to be created by the theme itself.

  8. Pingback: 如何分離 Comments 與 Pings | 我的普立茲

  9. Pingback: WordPress Theme Releases for 10/09 »   Weblog Tools Collection » Blog Archive

  10. Pingback: Separating Pings and trackbacks from Comments in WordPress 2.7

  11. Pingback: 如何分离WordPress2.7的comments与pings | 玩WordPress

  12. Pingback: WordPress Wednesday News: WordCamp News, WordPress 2.7, Lester Chan Plugins Famous, Super Cache Updated | The Blog Herald

  13. Pingback: WordPress Theme Releases for 10/09 | BlogBroker24-7

  14. Pingback: WordPress Theme Releases for 10/09 | PATRON DIGITAL.COM

  15. Pingback: WordPress Theme Releases for 10/09 | PATRONIT.NET

  16. Pingback: WordPress Wednesday News: WordPress 2.7 Freeze, WordPress Themes News, WordCamp Bangkok | The Blog Herald

  17. Pingback: WordPress Theme Releases for 10/09 · Softonix.com

  18. Pingback: WORDPRESS 2.7 DE A À Z

  19. Pingback: WordPress Theme Releases for 09.10.08 | PATRONIT.NET

  20. Pingback: WordPress Wednesday News: WordPress 2.7 Soon, Security Upgrade, PodCamp-WordCamp Hawaii, PollDaddy, and More | The Blog Herald

  21. Pingback: Wordpress 2.7 le nuove api dei commenti » Archivi Blog » WordPress Italy

  22. Monika says:

    Hi thanks a lot for this instructions – it saves me time ;)

    I have a question: Do you know where we can translate the new comments in our language? Because I would like to have another sentence for *your comment is awaiting ….* and I would like to have this in my language German ;)

    thanks
    Monika

  23. Pingback: WordPress 2.7 Separate 留言和样式化留言

  24. Pingback: Comment paging in WordPress 2.7 - nkuttler

  25. Pete says:

    Hey Matt, thanks for the info’s.

    However, this doesn’t work / works strange when comment paging is enabled. All Pingbacks/Trackbacks are listed only on the first page (the page with the oldest comments). In my case, they should be on every page after the comment form. Any workaround for this?

  26. Pingback: WordPress Wednesday News: WordPress 2.7 Beta 2, Danger WordPress Faker, and More WordCamps | The Blog Herald

  27. Pingback: WordPress 2.7 News « Lorelle on WordPress

  28. Pingback: WP2.7 Parent Child Themes | Visual Coma

  29. Aeolos says:

    Same issue with Pete. The code doesn’t play nice with paged comments.

  30. Aeolos says:

    Thanks for the info.
    Hopefully your code will still work without modification after the fix.
    Also sorry about the double comment earlier.

  31. Pingback: Wordpress 2.7 Checklist

  32. Pingback: WordPress Wednesday News: 2.7 Delayed, Sneak Peak Video, Help WordPress iPhone, WordCamps | The Blog Herald

  33. Pingback: WordPress 2.7 News | How to Use WordPress Fast

  34. Pingback: Blog 23r9i0 - Separar comentarios y trackback/pingback

  35. Pingback: Blog 23r9i0 - Separar comentarios y trackback-pingback wp2.7

  36. Pingback: WordPress Wednesday News: Beta 3 Released, WordCamp Australia, No WordPress 2.6.4, and More | The Blog Herald

  37. Pingback: WordPress 2.7 Release News and Links « Lorelle on WordPress

  38. Pingback: WordPress News: WordPress 2.7 New Login, WordCamp Australia, WordPress 2.6.5 Security Update, BuddyPress, and More | The Blog Herald

  39. Pingback: Gestion des commentaires pour wordpress 2.6 et wordpress 2.7

  40. Viper007Bond says:

    Everything should be working as intended now. Only issues you will run into by passing a type or a $comments array is that the “Recent Comments” widget will likely link to the wrong comment page. Not the end of the world though.

  41. Viper007Bond says:

    @Monika:

    Just use a custom callback function for displaying your comments rather than using the default one that wp_list_comments() provides.

    Basically have function like the list_pings() this post describes, but instead designed for comments.

  42. Pingback: WP 2.7 Blog Party is ON! | Human3rror - Where Typos are Part of the Equation

  43. Jauhari says:

    Is it possible to count the trackback too? How to do that?

  44. Pingback: Separating Comments and Trackbacks in WordPress - The Answer « Lorelle on WordPress

  45. Pingback: Kommentar und Trackback Count - dynamicinternet

  46. Ipstenu says:

    Since I hacked my wp_list_comments already to handle some formatting, I found this to work:

    <?php wp_list_comments(array('type'=>comment, 'avatar_size'=>48, 'reply_text'=>'Reply to this Comment')); ?>

    Now I just have to turn ping/trackbacks back on. Thanks!

  47. Pingback: WordPress 2.7 News | This Is The Maverick Of Blogs

  48. Thank you very much, it works like a treat.

  49. Works like a charm!

    Thanks for taking the time to write this up! Just used it on a theme on the latest 2.7, and works 100%

    Cheers

    Richelo

  50. Matt says:

    Thank you very much for this useful code. Although it’s pre 2.7, it seems to work! Lifesaver.

  51. Migs says:

    I would have to say this is the most straightforward and easy guide on how to separate comments from pings. Thanks!

  52. Pingback: Como estilizar comentários no WordPress usando funções callback | rbardini.com

  53. Pingback: Introducing Tweetbacks Plugin for Wordpress « ArticleSave

  54. Pingback: An Eclectic Mind » Interesting Links, January 11, 2009

  55. Pingback: AMB Album » Introducing Tweetbacks Plugin for Wordpress

  56. Pingback: links for 2009-01-12 - [LINICKX].com

  57. Pingback: Introducing Tweetbacks Plugin for Wordpress | The Blog Specialist

  58. Mike says:

    Hi
    Thanks for the tut.. One question!

    What do you do if your theme does not have a single.php file??

    Mike

  59. Pingback: Lo hice y lo entendí | Comentarios anidados con WordPress 2.7, nueva vista para archivos y otros cambios en la plantilla

  60. Pingback: Make Old Themes Compatible With Wordpress 2.7 Comment Features | eJabs

  61. Pingback: scriptygoddess » Wordpress wp_list_comments()

  62. Pingback: Introducing Tweetbacks Plugin for Wordpress | SuperBlog

  63. Alex says:

    @ Mike: check your index.php file for a line along the lines of:
    <?php if (!is_single()): ?>
    Code here
    <?php endif; ?>

    This indicates that the index will double as a single.php showing everything that is (different) within the is_single statement (sometimes also listed as is_singular). Hope this helps

    @ Matt: there is a minor issue with the last code snippet of yours, it can be that you get a “Fatal error: Only variables can be passed by reference in….” in your functions.php file using that. If someone has this problem I would suggest changing:
    add_filter('get_comments_number', 'comment_count', 0);
    function comment_count( $count ) {
    global $id;
    $comments_by_type = &separate_comments(get_comments('post_id=' . $id));
    return count($comments_by_type['comment']);
    }

    with
    add_filter('get_comments_number', 'comment_count', 0);
    function comment_count( $count ) {
    global $id;
    $get_comments= get_comments('post_id=' . $id);
    $comments_by_type = &separate_comments($get_comments);
    return count($comments_by_type['comment']);

    Cheers,
    ALEX

  64. Andreas says:

    Hello there! I see that you’ve done some nice and well-needed upgrades to the 1024px theme. Would you want to contribute to my forthcoming (and also well-needed) 2.7 compability upgrade and join me as a co-author of the theme? I could use some help, since I’ve overloaded with work and feeling that I’m not really able to keep the theme up to date. Contact me if you are interested! :)

  65. Pingback: Comment And Ping Count in WordPress 2.7 - WordPress 27, comments, Ping- and Trackbacks, Pings, Pingbacks, Comments - WP Engineer

  66. Andi Saleh says:

    Great step by step tutorial! It was really easy to implement.
    Thanks. :)

  67. Pingback: CoffeeBear.net » Blog Archive » Updating VectorLover Theme

  68. Pingback: Is WordPress 2.7 actually separating pings from comments? | Archive 2.0

  69. Pingback: Linkdump Februar 2009 [martin-grandrath.de]

  70. Pingback: January 2009 Links

  71. Pingback: Separating trackbacks from comments in WordPress 2.7 | Casey's Critical Thinking

  72. Pingback: WordPress 2.7 如何分離Comment/Trackbacks/Pingbacks | DreamersCorp

  73. woodman says:

    It is a good article,thanks for your sharing.

  74. Dayna says:

    I have used this method to separate my pings and comments, together with the wp comments navi plugin.

    For pages with pings, the wp comments navigation paging does not shows up. Any idea what might be happening? :(

    It worked for pages without pings though. Is the plugin incompatible with this method or..?

    Any help or advice is greatly appreciated. Thanks :)

  75. Stephanie says:

    Somehow I can’t get this to work. When I put the code in, my theme breaks and some posts – although they do have comments – do not display the comments anymore. And if I post a new test comment to a post with visible comments, all comments disappear. :-/ Any idea what I’m doing wrong?

  76. Everton says:

    great guide which I’ve implemented, but how do I get the pings to all go together on the last page rather than spread out over various pages e.g:
    http://www.connectedinternet.co.uk/2005/12“>Page onepage 2

    Thanks

  77. Everton says:

    Hi

    not sure if my previous comment made it. Great writeup but my trackbacks are seperated if I have comment pagination on e.g:

    - page 1
    - page 2

    How do I get all the trackbacks together?

    Thanks

    EB

  78. Pingback: How To Separate Pings from Comments In WordPress 2.7 | Connected Internet

  79. iPhone says:

    Thanks so much for this. I was a bit hesitant about editing the source but your instructions were easy to follow and I now have separated comments and pings!! Much better.

  80. Pingback: Separate your Comments and Pings in Wordpress 2.7 | Free iPhone

  81. Thanks so much for this. Have been using it for few blogs

  82. Pingback: Separate Trackbacks, Style Comments in WordPress 2.7

  83. Pingback: Separate Trackbacks, Style Comments in WordPress 2.7 | Guilda Blog

  84. I’m a noob at this, so don’t hate me when I say this..

    I don’t understand what you mean by create a callback function for the functions.php file. What code should I be putting in there exactly.

  85. Pingback: Separate Trackbacks, Style Comments in WordPress 2.7 | Online Money Earning Tips, Tricks, Info & Magic

  86. Pingback: Nuudelisoppa » Blog Archive » How to separate every other comment within a wp_list_comments callback function

  87. Pingback: Web design Speed | Web Development | Web Site Design | SEO | Tips » Wordpress wp_list_comments()

  88. Pingback: WordPress 2.7 Nested Comments With Separate Ping List | Armeda

  89. Pingback: Quick Blog Tip: Separating Comments and Trackbacks

  90. Pingback: Separating and Hiding Trackbacks with Jquery in WordPress 2.7 | Blog Tips, blogging Blog

  91. Geld says:

    Thanks for the information. I test it and it works fine.

  92. andol says:

    it is just what i want, thanx.

  93. Nice one. But will it work in 2.8?

  94. Pingback: Separating and Hiding Trackbacks with Jquery in WordPress 2.7 | Quest For News, A TUTORIAL Base

  95. Sebastian K. says:

    Thanks for this discription. Great step by step tutorial. It was very easy to implement :-)

  96. Pingback: Tweets als Kommentare in Wordpress anzeigen | Das Meinungs-Blog

  97. Pingback: Como estilizar los comentarios en WordPress 2.7 y 2.8 =A= Aeromental

  98. Pingback: Separating Trackbacks from Comments in WordPress 2.7+

  99. sahas says:

    did anyone find a solution to the weird paging issue when separating trackbacks/pingbacks from comments?

  100. Tom says:

    wow that’s great tutorial! thank you!

  101. Pingback: 晓闻心雨 » 在WordPress上将评论与Trackback分开显示

  102. Pingback: How to Separate Trackbacks from Comments in WordPress

  103. Pingback: Separare i commenti da pingback e trackback con Wordpress 2.7, 2.8 e superiori | BloGlobal.it

  104. Grey says:

    Thanks for the information Since I have updated my comment threads have been getting cluttered with Pings It was really getting annoying

  105. dave says:

    This is a great tutorial. Most of it works for me, but I am unable to get any of your functions.php code to work.

    When I insert this into my functions.php it breaks the site <li id="comment-">

    All I am doing is copy and pasting that in. Is there more to it? Should it go somewhere specific?

  106. Avery says:

    That is nice code.

  107. Pingback: Separating Trackbacks from Comments in WordPress 2.7+ | Download E-Books Free Video Training Courses Softwares

  108. surfy says:

    hello. I’m using wordpress 2.8.4 and I applied the changes of this tutorial. everything works fine except that the comment page. Unfortunately no longer works.

    Someone can tell me how to get it to work, taking the division?

    Thanks

    p.s: sorry for my bad english!

  109. Pingback: » 在WordPress上将评论与Trackback分开显示

  110. Anonymous says:

    I’ve never used PHP before and have no experience with it. Your tutorial worked like a charm on my WordPress 2.8.4 install. Thanks a million!

  111. Pingback: Devils Backyard » Blog Archive » Separating Trackbacks from Comments in WordPress 2.7+

  112. Nice tutorial. Unfortunately after implementing it, my comment form lost the nested comments function.

  113. Pingback: Separating Trackbacks from Comments in WordPress 2.7+ W3C Tag

  114. Atle says:

    After following this small guide all my comments all the sudden does’nt show. They are not deleted or anything, they just don’t show below my posts & pages anymore. No error eighter, accespt an error when submitting a new content. That error would be the usual one “Headers allready sent blablabla” witch WordPress seems to really like.

    Is this guide compatible with 2.8.5 ?

  115. Jim Clary says:

    Thank you for this information. I was going crazy trying to solve this problem.

  116. grabek says:

    Thanks!

    all well said, and I didn’t have to go to codex :)

  117. I wanted to personally thank you for posting this. I searched all around the web and only found updated articles regurgitating How-To’s on the old comments of WordPress… nothing helpful for 2.7+.

    This simple explaination made it simple to finally update my blogs. I wish WordPress would have made separation of Trackbacks/Pingbacks apart of the overhaul to the comments when they launch 2.7 versus putting it back on theme designers.

  118. Pingback: normalblog » Blog Archive » recent delicious bookmarks, September 25 through November 15

  119. Rob Flaherty says:

    If this is causing your comments or pings to not display at all, you just need to add this line to your comments.php file:

    <?php $comments_by_type = &separate_comments($comments); ?>

    Add it right before:
    <?php if ( !empty($comments_by_type['pings']) ) : ?>

  120. TopStreaming says:

    You aint got nothing on me…(run)…

  121. Laszlo says:

    Great tutorial, thank you.

  122. Josh says:

    When I implement the functions.php code to stop the count counting pings, the whole site breaks and goes blank.

    Any ideas on what could be wrong? I copied and pasted your code exactly.

  123. Boldis Media says:

    It’s cool, but not working with wordpress theme Monteray.

  124. pro-gadget says:

    wowhhh, this post is very2 complete..:D

  125. Greg says:

    Great, thanks a lot.

    But how do I insert a timestamp for every pinbback?

    - Posttitel – Blogname
    (7 February 2011 at 13:29)
    - Next Pingback – Another blog
    (8 February 2001 at 07:13)

  126. wowhhh, this post is very2 good ..:D

  127. Matt says:

    @Aeolos: I’m not sure how to fix that and haven’t looked but from a comment made by Viper007Bond in #wordpress-dev today:

    currently doing yet another comments overhaul. current code doesn’t work well when excluding pings or passing a custom $comments

    it looks like he is aware and working on fixing this in the core code.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre>