应网友的要求,奉上WordPress整站热门文章及分类热门文章的实现代码.其实这段代码也是Dream最开始也从网络搜索而来的,经过修改可以支持分类热门文章功能..实现原理是通过读取某篇文章的评论数来调用相关的文章,其实准确来说应该是”热评文章”.

应某位网友要求,另外附上该段代码下载:WordPress热门文章代码

/*
$termId:分类目录ID,为0时是检索所有分类目录
$posts_num:显示热评文章的数量
$days:检索多少天内的热评文章
*/
// 获得热评文章

function simple_get_most_review($termId=0,$posts_num=10, $days=30)
{
global $wpdb;
//所有热评文章
if($termId==0){
$sql = “SELECT `ID` , `post_title` , `comment_count` FROM $wpdb->posts
WHERE `post_type` = ‘post’
AND TO_DAYS( now( ) ) – TO_DAYS( `post_date` ) < $days
AND (`wp_posts`.`post_status` = ‘publish’ OR `wp_posts`.`post_status` = ‘inherit’)
ORDER BY `comment_count` DESC LIMIT 0 , $posts_num “;

}

//分类热评文章
else {
$sql=”SELECT `ID` , `post_title` , `comment_count`  FROM `wp_posts`
INNER JOIN `wp_term_relationships` ON (`wp_posts`.`ID` = `wp_term_relationships`.`object_id`)
INNER JOIN `wp_term_taxonomy` ON (`wp_term_relationships`.`term_taxonomy_id` =  `wp_term_taxonomy`.`term_taxonomy_id`)
WHERE   1=1
AND `wp_term_taxonomy`.`taxonomy` = ‘category’
AND `wp_term_taxonomy`.`term_id` = $termId
AND `wp_posts`.`post_type` = ‘post’
AND (`wp_posts`.`post_status` = ‘publish’ OR `wp_posts`.`post_status` = ‘inherit’)
GROUP BY `wp_posts`.`ID`
ORDER BY `comment_count` DESC LIMIT 0 , 10  “;

}
$posts = $wpdb->get_results($sql);
$output = “”;
foreach ($posts as $post){
$overPost=$post->post_title;
$output .= “\n<li><a href= \””.get_permalink($post->ID).”\” rel=\”bookmark\” title=\””.$post->post_title.”\” >”.$overPost.”</a></li>”;
}
echo $output;
}

将上面代码加到你的functions.php文件里,然后在需要调用热门文章的地方函数就行了.
所有分类热评文章:

<?php if (function_exists(‘simple_get_most_review’)) {simple_get_most_review(0,10,31); }  ?>

单个分类文章热评:

<?php if (function_exists(”simple_get_most_review’)) {simple_get_most_review($category->term_id,10,31); }  ?>


2010-10-28 BUG修复(已更新到上面代码及下载链接)…………………………………….

-在查找文章的时候增加了过滤条件,只查询已发布的文章,这样就不会把一些预发布、草稿、回收站里的文章也加进来。

AND (`wp_posts`.`post_status` = ‘publish’ OR `wp_posts`.`post_status` = ‘inherit’)

做人要厚道,转载请注明文章来源: https://www.boxui.com/blog-history/1297.html