• 折腾
  • Typecho 获取文章第一张图片

functions.php中加入

function thumb($cid) {
if (empty($imgurl)) {
$imgurl = "/usr/themes/hcms/images/log/".rand(1,5).".jpg";
}
$db = Typecho_Db::get();
$rs = $db->fetchRow($db->select('table.contents.text')
->from('table.contents')
->where('table.contents.type = ?', 'attachment')
->where('table.contents.parent= ?', $cid)
->order('table.contents.cid', Typecho_Db::SORT_ASC)
->limit(1));
$img = unserialize($rs['text']);
if (empty($img)){
echo $imgurl;
}
else{
echo $img['path'];
}
}

调用代码:<?php echo thumb($this->cid); ?>
其中$imgurl = "/usr/themes/hcms/images/log/".rand(1,5).".jpg";表示如果没有图片则随机加载/usr/themes/hcms/images/log/1-5.jpg