最近有不少用户咨询如何根据评论数获取指定天数热评文章?今天为大家分享获取指定天数热评文章WordPress教程,希望能够帮助到需要的朋友。
将下方代码添加进functions.php外加一个php文件即可实现:
//获取指定天数热评文章
function most_comm_posts($days=7, $nums=10) {
global $wpdb;
$today = date(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”Y-m-d H:i:s\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”);
$daysago = date( \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”Y-m-d H:i:s\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”, strtotime($today) – ($days * 24 * 60 * 60) );
$result = $wpdb->get_results(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”SELECT comment_count, ID, post_title, post_date FROM $wpdb->posts WHERE post_date BETWEEN \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’$daysago\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’ AND \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’$today\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’ ORDER BY comment_count DESC LIMIT 0 , $nums\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”);
$output = \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’;
if(!empty($result)) {
foreach ($result as $topten) {
$postid = $topten->ID;
$title = $topten->post_title;
$commentcount = $topten->comment_count;
if ($commentcount != 0) {
$output .= \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'<li><a href=\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’.get_permalink($postid).\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\” title=\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’.$title.\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”>\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’.$title.\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'</a> (\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’.$commentcount.\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’)</li>\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’;
}
}
}
echo $output;
}
//在需要调用的地方插入下方代码
<?php if(function_exists(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’most_comm_posts\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’)) most_comm_posts