get_results($sql);" />

wordpress获取热门文章的功能代码

南国老符 南国老符 最后更新于:2021-08-31 浏览:1849

wordpress获取热门文章的功能,把如下代码放到模板所在目录的functions.php里即可:


// 热门
function popular_posts($days = 7, $num = 10, $before='', $after=''){
    global $wpdb;
    $today = date("Y-m-d H:i:s"); //获取今天日期时间
    $daysago = date( "Y-m-d H:i:s", strtotime($today) - ($days * 24 * 60 * 60) );
    $sql = "SELECT comment_count,ID,post_title ";
    $sql .= "FROM $wpdb->posts where post_status='publish' and post_type='post' ";
    $sql .= " and post_date BETWEEN '$daysago' AND '$today' ";
    $sql .= "ORDER BY comment_count DESC ";
    $sql .= "LIMIT 0 , $num";
    $hotposts = $wpdb->get_results($sql);
    $output = '';
	$hot_key = 1;
    foreach ($hotposts as $hotpost) {
        $post_title = stripslashes($hotpost->post_title);
        $permalink = get_permalink($hotpost->ID);
		if($hot_key == 1){
			$output .= '<dd>';
			$output .= '	<div class="nex_readfirsts">';
			$output .= '		<div class="nex_readfirsts_num">' .$hot_key . '</div>';
			$output .= '		<div class="nex_readfirsts_info">';
			$output .= '			<h5>';
			$output .= 					$before.'<a href="' . $permalink . '">' . $post_title . '</a>' .$after;
			$output .= '			</h5>';
			$output .= '			<p>'.get_the_excerpt($hotpost->ID).'</p>';
			$output .= '		</div>';
			$output .= '		<div class="clear"></div>';
			$output .= '	</div>';
			$output .= '</dd>';
		}else{
			$output .= '<dd>';
			$output .= '	<div class="nex_readsecond_num">'.$hot_key.'</div>';
			$output .= '	<div class="nex_readingtxt">'.$before.'<a href="' . $permalink . '">' . $post_title . '</a>' .$after.'</div>';
			$output .= '	<div class="clear"></div>';
			$output .= '</dd>';
		}
		$hot_key ++;
    }
    if($output==''){
		//popular_posts($days = 5, $num = 10, $before='', $after='');
        $output .= $before.'no data...'.$after;
    }
    echo $output;
}

function hot_posts($days = 7, $offset = 0, $num = 10, $before='', $after='', $cat = 1){
    global $wpdb;
    $today = date("Y-m-d H:i:s"); //获取今天日期时间
    $daysago = date( "Y-m-d H:i:s", strtotime($today) - ($days * 24 * 60 * 60) );
    // $sql = "SELECT comment_count,ID,post_title ";
    $sql = "SELECT * ";
    $sql .= "FROM $wpdb->posts where post_status='publish' and post_type='post' ";
	if($cat != 1){
		$sql .= " and cat='$cat' and post_date BETWEEN '$daysago' AND '$today' ";
	}else{
		$sql .= " and post_date BETWEEN '$daysago' AND '$today' ";
	}
    $sql .= "ORDER BY comment_count DESC ";
    $sql .= "LIMIT $offset , $num";
    $hotposts = $wpdb->get_results($sql);
	return $hotposts;
}



点赞 (0)

收藏 (0)
转载请务必注明: 来源:《建站管家》https://www.df81.com/news/223.html

本站部分信息来源网络,如有侵权,请联系QQ:1062129401删除。

上一篇: wordpress更改固定连接URL后做301重定向的代码

下一篇: wordpress统计文章评论数功能代码

请先登录后,再发表评论 ~ ~
发表评论
评论 (0)

发表帖子
广告位招租( ¥5 / 天 )
点击咨询
最新帖子

南国老符
2025-05-26 16:54
南国老符
2024-09-29 10:29
南国老符
2023-12-15 21:41
广告位招租( ¥5 / 天 )
点击咨询
最近评论

U
user1379
回复:版本号多少?
U
user1379
回复:新版本修复了,下载新版本即可
U
user1379
U
user1304
回复:默认没有这个功能,你可以自行二次开发后期可能会考虑在站群版本上加入此功能及商城功能
Y
yuanis90
回复:后台->语言->全局管理->模型管理->编辑->启用关键词内链即可
Y
yuanis90
回复:后台已经启用了,但是前台产品说明的关分键词还是没有自动链接到,
购买VIP
购买授权
余额充值
发表帖子
客服微信