Wordpress Most Commented Post Plugin

This is a quick and dirty most comment post plugin. It is what i use on my site to drive the sidebar widget. I wrote this as i wanted something really simple. It generates a <ul> element with four styles that you can adjust from your style.css in your theme folder. i am working on a settings page and once that is complete, i will release it on wordpress.org the classes are set on the <li>s: .hot .less-hot .even-less-hot .least-hot the class on the <ul> is: .most-commented here is the example css that i use:
.most-commented { margin: 0; padding: 0; list-style-type: none; }
.most-commented li a { display: block }
.hot, .less-hot, .even-less-hot, .least-hot { padding: 8px 5px 8px 5px; color: #FFFFFF; text-align: left; }
.hot { background: #D7331E; margin-top: 10px; border-bottom: #801f12 1px solid; }
.less-hot { background: #d7681e; border-bottom: #803e12 1px solid; }
.even-less-hot { background: #d7981e; border-bottom: #805b12 1px solid; }
.least-hot { background: #d7c81e; border-bottom: #807612 1px solid; }

the math used to calculate the popularity of the post is set in the function crcp_display_most_comment_posts(); i chose 10%, 30%, 60% for the thresholds. feel free to change them as you see fit you can also adjust the LIMIT part of the sql query in the function crcp_get_most_commented_posts(); in order to adjust how many you want to show in your sidebar the last piece is to place <?php crcp_display_most_commented_posts(); ?> in your sidebar to activate it here is the plugin
<?php
/*
Plugin Name: Most Commented Posts
Plugin URI: http://www.matthewaprice.com
Description: Drives Most Commented Posts
Version: 0.5
Author: Matthew Price
Author URI: http://www.matthewaprice.com
License: GPL2
*/

function crcp_get_most_commented_posts() {

global $wpdb;

$comments = $wpdb->get_results( " SELECT COUNT(comment_post_ID) as count, comment_post_ID FROM " . $wpdb->prefix . "comments GROUP BY comment_post_ID ORDER BY count DESC LIMIT 4 " );
return $comments;                                                                               

}       

function crcp_display_most_commented_posts() {

$top_count = crcp_get_most_commented_post_count();
$comments = crcp_get_most_commented_posts();

$ten_percent = $comments[0]->count * 0.10;
$thirty_percent = $comments[0]->count * 0.30;
$sixty_percent = $comments[0]->count * 0.60;


        echo "<ul class="most-commented">";

        foreach ($comments as $comment) {

                $title_class = '';
                
                if ($comment->count >= $sixty_percent) { $title_class = "hot"; }
                elseif ( ($comment->count <= $sixty_percent) && ($comment->count >= $thirty_percent) ) { $title_class = "less-hot"; }
                elseif ( ($comment->count < $thirty_percent) && ($comment->count >= $ten_percent) ) { $title_class = "even-less-hot"; }
                elseif ($comment->count < $ten_percent) { $title_class = "least-hot"; }
                                                                
                $comment_post_id = $comment->comment_post_ID;        
                query_posts("p=$comment_post_id");
                
                                if ( have_posts() ) : while ( have_posts() ) : the_post();
                                        $post_title = get_the_title();
                                        $permalink = get_permalink($comment_post_id);
                                        echo "<li class="" . $title_class . ""><a href="" . $permalink . "">" . $post_title . "</a></li>";
                                endwhile;
                                endif;  
                
                wp_reset_query();       
                        
        }

        echo "</ul>";

}               

?>

Post a comment





Real Time Web Analytics ^