Typecho首页和分类页索引显示缩略图的方法
[朗读900字]
一、将下面这段代码贴到functions.php里面去。
//获取文章缩略图
function showThumbnail($widget)
{
$rand = rand(1,99); // 随机 1-99 张缩略图
$random = $widget->widget('Widget_Options')->themeUrl . '/img/sj/' . $rand . '.jpg'; // 随机显示缩略图
// $random = $widget->widget('Widget_Options')->themeUrl . '/img/no.jpg'; // 只显示一张默认缩略图
$attach = $widget->attachments(1)->attachment;
$pattern = '/\<img.*?src\=\"(.*?)\"[^>]*>/i';
if (preg_match_all($pattern, $widget->content, $thumbUrl)) {
echo '<img class="thumb" src="'.$thumbUrl[1][0].'" />';
} elseif ($attach->isImage) {
echo '<img class="thumb" src="'.$attach->url.'" />';
}
else {
echo '<img class="thumb" src="'.$random.'" />';
}
}
二、打开 index.php 和 archive.php
分别在<div class="post-content" itemprop="articleBody"> 前面加上 <div class="post-thumb"><?php showThumbnail($this); ?></div>
三、在style.css中添加如下样式。
.post-thumb {
float: left;
width: 150px;
max-height: 120px;
overflow: hidden;
margin: 0 10px 10px 0;
border-radius: 5px;
border:1px solid #ccc;
}
.thumb{width: 150px;
max-height: 120px;}
@media (max-width: 768px) {
.post-thumb{width:100%!important;}
.thumb{width:100%!important;}
}