在制作wordpress博客主题的时候常常需要调用wordpress热评文章

使用插件的话,会给服务器带来负荷

空间配置不高的话,打开网页的速度也会受到影响。

下面给大家介绍非插件调用wordpress热评文章

首先在functions.php加入如下代码:

function most_popular_posts($no_posts = 10, $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;
}
然后在主题文件中要调用热评文章的位置加入下面调用标签即可:
<?php most_popular_posts(); ?>


相关文章: