Typecho 获取文章第一张图片做缩略图
No Reply , Posted in 代码 on April 9, 2014
- 编写 functions.php 中的代码(给出的图片地址是绝对地址,如果想要相对地址可以使用php操作字符串进行截断。)
function img_postthumb($cid) { $db = Typecho_Db::get(); $rs = $db->fetchRow($db->select('table.contents.text') ->from('table.contents') ->where('table.contents.cid=?', $cid) ->order('table.contents.cid', Typecho_Db::SORT_ASC) ->limit(1)); preg_match_all("/\]*>/i", $rs['text'], $thumbUrl); //通过正则式获取图片地址 $img_src = $thumbUrl[1][0]; //将赋值给img_src $img_counter = count($thumbUrl[0]); //一个src地址的计数器 switch ($img_counter > 0) { case $allPics = 1: echo $img_src; //当找到一个src地址的时候,输出缩略图 break; default: echo ""; //没找到(默认情况下),不输出任何内容 }; }
- 调用代码
<img src="<?php echo img_postthumb($this->cid); ?>">
转自:typecho之家