文章采集调用(非调用wordpress热评热评文章的制作文章 )
优采云 发布时间: 2021-12-16 16:06文章采集调用(非调用wordpress热评热评文章的制作文章
)
在制作wordpress博客主题时,经常需要调用wordpress热评论文章
如果你使用插件,它会加载服务器
如果空间配置不高,打开网页的速度也会受到影响。
给大家介绍一下非插件调用wordpress的热评文章
首先在functions.php中添加以下代码:
function most_popular_posts($no_posts = 10, $before = '', $after = '', $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 . '' . $post_title . ' (' . $comment_count.')' . $after;
}
//输出文章列表项
} else {
$output .= $before . "None found" . $after;
//没有文章时则输出
}
echo $output;
}
然后在主题文件中要调用热评论文章的位置添加如下调用标签: