one possibility is:
public function popularPosts($num)
{
global $post;
$args = array( 'orderby' => 'comment_count', 'order' => 'DESC' );
$posts = get_posts( $args );
$count = 0;
foreach ($posts as $post):
if($post->comment_count != 0 ){
$count++;
}
endforeach;
if ( $count > 0 ):
foreach( $posts as $post ) : setup_postdata($post);
if($post->comment_count != 0 ){
?>
<li><a href="https://wordpress.stackexchange.com/questions/83444/<?php the_permalink(); ?>"><?php the_title(); ?></a> - <?php echo $post->comment_count; ?> Comments</li>
<?php
}
endforeach;
else :
echo '<li>No commented post this time!!</li>';
endif;
}
}
function swpopularpostwidget_init() {
if ( !is_blog_installed() )
return;
register_widget('swpopularpostwidget');
do_action('widgets_init');
}
add_action( 'init' , 'swpopularpostwidget_init');