获取文章第一张图作为cover

This commit is contained in:
浪子 2024-05-18 18:51:07 +08:00
parent d8cf19ca6d
commit fc3d9b6890
2 changed files with 14 additions and 2 deletions

View File

@ -156,3 +156,11 @@ function allwords() {
elseif ($chars>1000000){ elseif ($chars>1000000){
echo '全站共 '.$chars.' 字,已写一本列夫·托尔斯泰的《战争与和平》了!';} echo '全站共 '.$chars.' 字,已写一本列夫·托尔斯泰的《战争与和平》了!';}
} }
function show_first_image($content) {
preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $content, $matches);
// 检查是否找到了图片
if(isset($matches[1][0])){
return $matches[1][0];
}
return false; // 没有找到图片,返回 false
}

View File

@ -56,6 +56,10 @@
<a href="<?php $this->permalink() ?>#comments"><?php $this->commentsNum('评论', '1 评论', '%d 评论'); ?></a> <a href="<?php $this->permalink() ?>#comments"><?php $this->commentsNum('评论', '1 评论', '%d 评论'); ?></a>
</div> </div>
</div> </div>
<?php $firstImage = show_first_image($this->content); ?>
<?php if($firstImage): ?>
<img src="<?php echo $firstImage; ?>" alt="文章图片" class="cover"/>
<?php endif; ?>
</article> </article>
<?php endwhile; ?> <?php endwhile; ?>