Typecho文章图片附件缩略图实现代码

在function.php里添加

function thumb($obj) { $rand_num = 10; //随机图片数量,根据图片目录中图片实际数量设置 if ($rand_num == 0) { $imgurl = "随机图片存放目录/0.jpg"; //如果$rand_num = 0,则显示默认图片,须命名为"0.jpg",注意是绝对地址 }else{ $imgurl = "随机图片存放目录/".rand(1,$rand_num).".jpg"; //随机图片,须按"1.jpg","2.jpg","3.jpg"...的顺序命名,注意是绝对地址 } $attach = $obj->attachments(1)->attachment; if(isset($attach->isImage) && $attach->isImage == 1){ $thumb = $attach->url; }else{ $thumb = $imgurl; } return $thumb; }

在需要处引用

<img src="<?php echo thumb($this); ?>"/>

猜你喜欢

转载自www.cnblogs.com/idid/p/12421798.html
今日推荐