Warning: Invalid argument supplied for foreach() in wp-content\themes\magonline\functions.php on line 621Now first of all I will describe why the error is coming up. This error shows up when there are no comments in the database, since there are no comments hence the system cannot find a top commentator. A scenario that should have been taken care of, as this is the simplest of them all. So now we know why the error was showing up we can get busy with how to fix it. We have the following two solutions:
The Simplest Solution (If you are not tech savvy or are in a hurry)
Just add a comment. This could be any comment on any post and the error will be gone.
The Proper Solution
This solution requires a good understanding of PHP. If you go in to the file mentioned in the error i.e. wp-content\themes\magonline\functions.php, you will find a function top_commentators the following code:
function top_commentators($limit=8){
global $wpdb, $post;
//$top = $wpdb->get_results("SELECT $wpdb->comments.comment_author, comment_author_url, comment_ID, COUNT($wpdb->comments.comment_post_ID) AS 'comment_total' FROM $wpdb->comments WHERE comment_approved = '1' LEFT JOIN $wpdb->comments ON $wpdb->comments.comment_author = $wpdb->comments.comment_author WHERE comment_approved = '1' LIMIT 4");
$top = $wpdb->get_results("SELECT * FROM $wpdb->comments");
foreach ($top as $t){
$comm[$t->comment_author][] = $t;
$commlink[$t->comment_author] = $url = $t->comment_author_url=="http://"?"mailto:".$t->comment_author_email:$t->comment_author_url;
}
$x=0;
foreach ($comm as $t2n=>$t2v){
if ($x<=$limit){ $comm2[$t2n] = sizeof($t2v); } $x++; } foreach ($comm2 as $t3n=>$t3v){
echo '' . $t3n . ' (' . $t3v . ')';
}
}
just replace it with the following:
function top_commentators($limit=8){
global $wpdb, $post;
//$top = $wpdb->get_results("SELECT $wpdb->comments.comment_author, comment_author_url, comment_ID, COUNT($wpdb->comments.comment_post_ID) AS 'comment_total' FROM $wpdb->comments WHERE comment_approved = '1' LEFT JOIN $wpdb->comments ON $wpdb->comments.comment_author = $wpdb->comments.comment_author WHERE comment_approved = '1' LIMIT 4");
$top = $wpdb->get_results("SELECT * FROM $wpdb->comments");
// Looks like the plug-in writers forgot to handle the scenario when there are no comments in the Wordpress database
if(!empty($top))
{
foreach ($top as $t)
{
$comm[$t->comment_author][] = $t;
$commlink[$t->comment_author] = $url = $t->comment_author_url=="http://"?"mailto:".$t->comment_author_email:$t->comment_author_url;
}
$x=0;
foreach ($comm as $t2n=>$t2v)
{
if ($x<=$limit) { $comm2[$t2n] = sizeof($t2v); } $x++; } foreach ($comm2 as $t3n=>$t3v)
{
echo '' . $t3n . ' (' . $t3v . ')';
}
}// end of if(!empty($top))
}
And you are good to go. The code has not changed simply a check has been added before the the loops, just to make sure that the result set that is being fetched from MySQL is not empty and that does the trick. Hope this helps :)