Hi,
Please use this code to display the posts that have received the most comments. Youl need to place this code in your sidebar where you want to be displayed:
1) Open your theme header.php file and add this code within <head></head> tag:
<?php most_popular_posts($no_posts = 5, $before = ‘<li>’, $after = ‘</li>’,
$show_pass_post = false, $duration=”)
{
global $wpdb;
$request = “SELECT ID, post_title, COUNT($wpdb->comments.comment_post_ID)
AS ‘comment_count’ FROM $wpdb->posts, $wpdb->comments”;
$request .= ” WHERE comment_approved = ‘1′ AND $wpdb->posts.ID=$wpdb-
>comments.comment_post_ID AND post_status = ‘publish’”;
if(!$show_pass_post) $request .= ” AND post_password =””;
if($duration !=”") { $request .= ” AND DATE_SUB(CURDATE(),INTERVAL
“.$duration.” DAY) < post_date “;
}
$request .= ” GROUP BY $wpdb->comments.comment_post_ID ORDER BY
comment_count DESC LIMIT $no_posts”;
$posts = $wpdb->get_results($request);
$output = ”;
if ($posts) {
foreach ($posts as $post) {
$post_title = stripslashes($post->post_title);
$comment_count = $post->comment_count;
$permalink = get_permalink($post->ID);
$output .= $before . ‘<a href=”‘ . $permalink . ‘” title=”‘ . $post_title.’”>’ .
$post_title . ‘</a> (’ . $comment_count.’)’ . $after;
}
} else {
$output .= $before . “None found” . $after;
}
echo $output;
} ?>
If you want to display more than 5 posts which have received most comments on the post then you can change the value of $no_posts = 5 with your desired post number.
Now, add this code where you want to display the most commented post:
<?php most_popular_posts(); ?>
Generally, it is better to display most commented post in sidebar.
Thanks,
Shane G.
Related posts:
- Code To Display Recent Posts from Specific Categories In WordPress Blog Hi, In many cases we do require to display the…
- Code To Display Total Number of Trackbacks In WordPress Blog Hi, Here is the code to display total number of…
- Code To Display Recent Posts In WordPress Blog Hello All, In order to display recent posts of wordpress…
Loading...
Be First To Comment
Related Post
Leave Your Comments Below