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.

141 thoughts on “Separating Pings from Comments in WordPress 2.7

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

    • 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'];

  2. 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 :-)

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

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

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

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

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

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

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

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

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

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

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

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

  15. Pingback: WORDPRESS 2.7 DE A À Z

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

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

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

  19. 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

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

  21. Pingback: Comment paging in WordPress 2.7 - nkuttler

  22. 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?

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

  24. Pingback: WordPress 2.7 News « Lorelle on WordPress

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

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

  27. Pingback: Wordpress 2.7 Checklist

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

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

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

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

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

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

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

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

  36. 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.

  37. @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.

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

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

  40. Pingback: Kommentar und Trackback Count - dynamicinternet

  41. 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!

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

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

  44. Pingback: Introducing Tweetbacks Plugin for Wordpress « ArticleSave

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

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

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

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

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

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

  51. Pingback: scriptygoddess » Wordpress wp_list_comments()

  52. Pingback: Introducing Tweetbacks Plugin for Wordpress | SuperBlog

  53. @ 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

  54. 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! :)

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

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

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

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

  59. Pingback: January 2009 Links

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

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

  62. 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 :)

  63. 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?

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

  65. 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.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  82. 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?

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

  84. 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!

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

  86. 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!

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

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

  89. 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 ?

  90. 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.

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

  92. 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']) ) : ?>

  93. 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.

  94. 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)

  95. @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>