WordPress: RETURN comment count

May 25, 2004
6:38 am
Posted in: General, Tech, WordPress

One of the very few gripes I have with WordPress is that some functions ECHO a variable, and others RETURN it. The naming system is all over the place, so you can’t look at a function and say “get_comment_count(); must return the value, and show_comment_count(); must display it!” I wanted to perform some logic on my entries with the comment count. WordPress has a function to echo the count, and you can pass strings to it such as “no comments” and “1 comment” and “% comments” so it displays correctly, but the result is echoed.

Of course, my favorite thing about WordPress is the really really REALLY simple plugin system. I looked at the function that echoes the comment count (comments_number();), changed it to do what I wanted, called the function “get_comments_count();” and saved it as a PHP file in my plugins directory. One quick trip into WordPress to activate the plugin, and I was in business.

Here’s the plugin:

<?php
/*
Plugin Name: Get Comments Count
Version: 1.0
Plugin URI: http://txfx.net/
Description: Returns (not displays!) the current number of comments.  Use it in "the loop."
Author: Mark Jaquith
Author URI: http://txfx.net/
*/

function get_comments_count() {
global $id, $comment, $tablecomments, $wpdb, $comment_count_cache;
if ('' == $comment_count_cache["$id"]) $number = $wpdb->get_var("SELECT COUNT(*) FROM $tablecomments WHERE comment_post_ID = $id AND comment_approved = '1'");
else $number = $comment_count_cache["$id"];
return $number;
}
?>

And a final thought on the subject:

if($wordpress_function_naming_system == 'dodgy') {
blame_photomatt(); // Just kidding!  ;-) 
}

7 Responses to “WordPress: RETURN comment count”

  1. scriptygoddess says:

    May 30 2004 Bookmarks: Return comment count plugin Mark came up with a nice little (wordpress) plugin to simply return th… [IMG :) ] Posted by Jennifer [IMG go to individual entry page] | [IMG comments] {2} | [IMG add this entry to your hotlist]

  2. JavaScript into the head of your WordPress pages. Although it uses that JavaScript, no modification of the plugin is needed, we can handle all of this by modifying our index.php and wp-comments.php files. It also requires my Get Comments Count plugin.

  3. Diva says:

    I don’t think that I quite understand your instructions… Where do you put the code? I uploaded your script to my plug in folder and I activated it… Where and how do I call the function?

  4. Links says:

    WP Plugins…

    WP Plugins Weblog Tools Collection idly.org – WPLinkblogger 0.1 Meyerweb: Tools: WordPress Plugins and Hacks Comment Killer Kitten’s Project Blog – Plugins scriptygoddess, a variety of plugins and links to plugins Tempus Fugit | TxFx.net | WordPress: …

  5. muttznutz says:

    Hiya,
    Thanks for this plugin, it was just what I was looking for.

  6. [...] also used the Get Comments Count plugin from Mark Jaquith’s Tempus Fugit site. It should make posts summaries cleaner, by only [...]

Leave a Reply

Note: If you are replying to another commenter, click the "Reply to {NAME} →" button under their comment to help the conversation have a better flow!

Anti-Spam Quiz: