Tick tock...

  WordPress: RETURN comment countArchives » 2004 » May » 25 » WordPress: RETURN comment count

May 25th, 2004

WordPress: RETURN comment count

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://www.txfx.net/
Description: Returns (not displays!) the current number of comments.  Use it in "the loop."
Author: Mark Jaquith
Author URI: http://www.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! ;-)
}
Posted by Mark @ 6:38 am in "Weblogs"
PermalinkPermalink | Add a commentAdd a comment
Comments

There are no comments for this entry. Why not make the first one?

Post a Comment

TrackBack URI for this entry: http://www.txfx.net/2004/05/25/get-comment-count/trackback/

Line and paragraph breaks automatically, e-mail address is never displayed, so don't bother garbling it, HTML allowed: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <del datetime=""> <em> <i> <strike> <strong>


(required)


(required)



Comment Preview:

  • When you are ready to post your comment, click the "Post Comment" button ONCE, and wait.
  • All comments are subject to the rules.
die spambots!