emlog缩略图不同实现方法:正文-附件-随机图片

2019年3月26日22:28:37 发表评论 594 views

实现emlog列表也缩略图按先调用正文第一张图片,没有则调用附件图片,如果前两者都没有则调用随机图片代码

 

第一步:首先在模板的module.php文件中最后面加入如下代码

<?php
//全局匹配正文中的图片并存入imgsrc中
function img_zw($content){preg_match_all("|<img[^>]+src=\"([^>\"]+)\"?[^>]*>|is", $content, $img);$imgsrc = !empty($img[1]) ? $img[1][0] : '';if($imgsrc):return $imgsrc;endif;}
//Custom: 获取附件第一张图片
function img_fj($logid){$db = MySql::getInstance();$sql = "SELECT * FROM ".DB_PREFIX."attachment WHERE blogid=".$logid." AND (`filepath` LIKE '%jpg' OR `filepath` LIKE '%gif' OR `filepath` LIKE '%png') ORDER BY `aid` ASC LIMIT 0,1";$imgs = $db->query($sql);$img_path = "";while($row = $db->fetch_array($imgs)){$img_path .= BLOG_URL.substr($row['filepath'],3,strlen($row['filepath']));}
return $img_path;}?>

第二步:然后在log_list.php的foreach循环中加入如下代码

<?php if(img_zw($value['content'])){$imgurl = img_zw($value['content']);
}elseif(img_fj($value['logid'])){$imgurl = img_fj($value['logid']);
}else{$imgurl = TEMPLATE_URL.'images/'.rand(1,5).'.jpg';}
?><p><img src="<?php echo $imgurl;?>" /></p>

历史上的今天:


欢迎来到菜鸟头头的个人博客
本文章百度已收录,若发现本站有任何侵犯您利益的内容,请及时邮件或留言联系,我会第一时间删除所有相关内容。

  • A+
所属分类:WEB

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: