前面分享过typecho猎取文章首张图片作为缩略图的代码,此次分享的代码则是挪用文章首个图片附件作为缩略图,假如不存在图片附件时,则随机显现预先设置好的默许图片。
操纵步骤:
1、在主题functions.php文件中增加代码:
1234567891011121314151617 | 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;} |
2、挪用代码:
1 | <img src="<?php echo thumb($this); ?>"/> |

评论列表