获取文章第一张图作为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

@ -155,4 +155,12 @@ function allwords() {
echo '全站共 '.$chars.' 字,写完一本我国著名的四大名著了!';}
elseif ($chars>1000000){
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

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