鸿 网 互 联 www.68idc.cn

当前位置 : 服务器租用 > cms安装教程 > WordPress > >

wordpress实现评论排行榜

来源:互联网 作者:佚名 时间:2015-10-18 05:55
?php function popular_posts($no_posts = 5, $before = 'ulli', $after = '/ul', $show_pass_post = false, $duration='') {global $wpdb;$request = "SELECT ID, post_title, COUNT($wpdb-comments.comment_post_ID) AS 'comment_count' FROM $wpdb-posts,
<?php function popular_posts($no_posts = 5, $before = '<ul><li>', $after = '</ul>', $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' AND $wpdb->posts.post_type != 'page'";
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;
} ?>
<?php popular_posts(); ?>

把这段代码直接加到widget里就可以了,前提是要先安装exec-php插件,这段代码是从网上找的,稍微修改了一些小缺陷:

1.在$before和$after变量开始处和结束处分别添加了<ul>标签和</ul>标签,这样能使文章标题列表按照wordpress定义的样式显示。

2.SQL查询语句中加入了AND $wpdb->posts.post_type != 'page',用于屏蔽对页面的评论数显示。

网友评论
<