返回顶部

Typecho最新1.2 代码实现文章缩略图

在主题functions.php文件中添加以下代码:

function getArticleOneImg($widget){
  $pattern = '/\<img.*?src\=\"(.*?)\"[^>]*>/i';
   if (preg_match_all($pattern, $widget->content, $thumbUrl) && strlen($thumbUrl[1][0]) > 7) {
     return $thumbUrl[1][0];
   }
}

(通过正则表达式匹配文章内容的img标签获取src的图片地址。)


调用代码

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


暂无评论