This commit is contained in:
浪子 2024-06-06 11:56:02 +08:00
parent 6473985801
commit dc59ba972e
3 changed files with 25 additions and 20 deletions

View File

@ -171,13 +171,23 @@ function allwords() {
elseif ($chars>1000000){ elseif ($chars>1000000){
echo '全站共 '.$chars.' 字,已写一本列夫·托尔斯泰的《战争与和平》了!';} echo '全站共 '.$chars.' 字,已写一本列夫·托尔斯泰的《战争与和平》了!';}
} }
function show_first_image($content) { function img_postthumb($cid) {
preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $content, $matches); $db = Typecho_Db::get();
// 检查是否找到了图片 $rs = $db->fetchRow($db->select('table.contents.text')
if(count($matches) > 0 && count($matches[0]) > 0 ){ ->from('table.contents')
return $matches[1][0]; ->where('table.contents.cid=?', $cid)
->order('table.contents.cid', Typecho_Db::SORT_ASC)
->limit(1));
// 检查是否获取到结果
if (!$rs) {
return "";
}
preg_match_all("/https?:\/\/[^\s]*.(png|jpeg|jpg|gif|bmp|webp)/", $rs['text'], $thumbUrl); //通过正则式获取图片地址
// 检查是否匹配到图片URL
if (count($thumbUrl[0]) > 0) {
return $thumbUrl[0][0]; // 返回第一张图片的URL
} else { } else {
return false; // 没有找到图片,返回 false return ""; // 没有匹配到图片URL返回空字符串
} }
} }
//开始增加某些奇怪的东西 //开始增加某些奇怪的东西

View File

@ -19,21 +19,20 @@
d="M12 9C10.3431 9 9 10.3431 9 12C9 13.6569 10.3431 15 12 15C13.6569 15 15 13.6569 15 12C15 10.3431 13.6569 9 12 9ZM11 12C11 11.4477 11.4477 11 12 11C12.5523 11 13 11.4477 13 12C13 12.5523 12.5523 13 12 13C11.4477 13 11 12.5523 11 12Z" /> d="M12 9C10.3431 9 9 10.3431 9 12C9 13.6569 10.3431 15 12 15C13.6569 15 15 13.6569 15 12C15 10.3431 13.6569 9 12 9ZM11 12C11 11.4477 11.4477 11 12 11C12.5523 11 13 11.4477 13 12C13 12.5523 12.5523 13 12 13C11.4477 13 11 12.5523 11 12Z" />
<path fill-rule="evenodd" clip-rule="evenodd" <path fill-rule="evenodd" clip-rule="evenodd"
d="M21.83 11.2807C19.542 7.15186 15.8122 5 12 5C8.18777 5 4.45796 7.15186 2.17003 11.2807C1.94637 11.6844 1.94361 12.1821 2.16029 12.5876C4.41183 16.8013 8.1628 19 12 19C15.8372 19 19.5882 16.8013 21.8397 12.5876C22.0564 12.1821 22.0536 11.6844 21.83 11.2807ZM12 17C9.06097 17 6.04052 15.3724 4.09173 11.9487C6.06862 8.59614 9.07319 7 12 7C14.9268 7 17.9314 8.59614 19.9083 11.9487C17.9595 15.3724 14.939 17 12 17Z" /> d="M21.83 11.2807C19.542 7.15186 15.8122 5 12 5C8.18777 5 4.45796 7.15186 2.17003 11.2807C1.94637 11.6844 1.94361 12.1821 2.16029 12.5876C4.41183 16.8013 8.1628 19 12 19C15.8372 19 19.5882 16.8013 21.8397 12.5876C22.0564 12.1821 22.0536 11.6844 21.83 11.2807ZM12 17C9.06097 17 6.04052 15.3724 4.09173 11.9487C6.06862 8.59614 9.07319 7 12 7C14.9268 7 17.9314 8.59614 19.9083 11.9487C17.9595 15.3724 14.939 17 12 17Z" />
</svg><span class="article--views"><a><?php get_post_view($this) ?></a></span> 阅读 </svg><span class="article--views"><a><?php get_post_view($this) ?></a></span>
<svg viewBox="0 0 24 24" class="icon" aria-hidden="true" width="16" height="16"> <svg viewBox="0 0 24 24" class="icon" aria-hidden="true" width="16" height="16">
<g> <g>
<path <path
d="M1.751 10c0-4.42 3.584-8 8.005-8h4.366c4.49 0 8.129 3.64 8.129 8.13 0 2.96-1.607 5.68-4.196 7.11l-8.054 4.46v-3.69h-.067c-4.49.1-8.183-3.51-8.183-8.01zm8.005-6c-3.317 0-6.005 2.69-6.005 6 0 3.37 2.77 6.08 6.138 6.01l.351-.01h1.761v2.3l5.087-2.81c1.951-1.08 3.163-3.13 3.163-5.36 0-3.39-2.744-6.13-6.129-6.13H9.756z"> d="M1.751 10c0-4.42 3.584-8 8.005-8h4.366c4.49 0 8.129 3.64 8.129 8.13 0 2.96-1.607 5.68-4.196 7.11l-8.054 4.46v-3.69h-.067c-4.49.1-8.183-3.51-8.183-8.01zm8.005-6c-3.317 0-6.005 2.69-6.005 6 0 3.37 2.77 6.08 6.138 6.01l.351-.01h1.761v2.3l5.087-2.81c1.951-1.08 3.163-3.13 3.163-5.36 0-3.39-2.744-6.13-6.129-6.13H9.756z">
</path> </path>
</g> </g>
</svg> <a href="<?php $this->permalink() ?>#comments"><?php $this->commentsNum('评论', '1 评论', '%d 评论'); ?></a> </svg> <a href="<?php $this->permalink() ?>#comments"><?php $this->commentsNum('0', '1', '%d '); ?></a>
<?php if($this->user->hasLogin() && $this->user->pass('editor', true)): ?> <?php if($this->user->hasLogin() && $this->user->pass('editor', true)): ?>
<svg class="icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512" width="16" height="16"> <svg class="icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512" width="16" height="16">
<!--!Font Awesome Free 6.5.2 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free Copyright 2024 Fonticons, Inc.-->
<path <path
d="M362.7 19.3L314.3 67.7 444.3 197.7l48.4-48.4c25-25 25-65.5 0-90.5L453.3 19.3c-25-25-65.5-25-90.5 0zm-71 71L58.6 323.5c-10.4 10.4-18 23.3-22.2 37.4L1 481.2C-1.5 489.7 .8 498.8 7 505s15.3 8.5 23.7 6.1l120.3-35.4c14.1-4.2 27-11.8 37.4-22.2L421.7 220.3 291.7 90.3z"/> d="M362.7 19.3L314.3 67.7 444.3 197.7l48.4-48.4c25-25 25-65.5 0-90.5L453.3 19.3c-25-25-65.5-25-90.5 0zm-71 71L58.6 323.5c-10.4 10.4-18 23.3-22.2 37.4L1 481.2C-1.5 489.7 .8 498.8 7 505s15.3 8.5 23.7 6.1l120.3-35.4c14.1-4.2 27-11.8 37.4-22.2L421.7 220.3 291.7 90.3z"/>
</svg> </svg>
<a href="<?php $this->options->adminUrl('write-post.php?cid=' . $this->cid); ?>" target="_blank" title="编辑文章">编辑文章</a> <a href="<?php $this->options->adminUrl('write-post.php?cid=' . $this->cid); ?>" target="_blank" title="编辑文章">Edit</a>
<?php endif; ?> <?php endif; ?>
</div> </div>
<h2 class="post--single__title"><?php $this->title() ?></h2> <h2 class="post--single__title"><?php $this->title() ?></h2>

View File

@ -57,12 +57,8 @@
</div> </div>
</div> </div>
<?php <?php
// 从文章内容中提取第一张图片 $firstImage = img_postthumb($this->cid);
$firstImage = show_first_image($this->content);
// 检查自定义字段cover是否已设置
$cover = $this->fields->cover; $cover = $this->fields->cover;
// 如果自定义字段cover已设置则优先使用
// 如果没有设置,则显示文章的第一张图片
$imageToDisplay = !empty($cover) ? $cover : $firstImage; $imageToDisplay = !empty($cover) ? $cover : $firstImage;
if($imageToDisplay): ?> if($imageToDisplay): ?>
<img src="<?php echo $imageToDisplay; ?>" alt="文章图片" class="cover"/> <img src="<?php echo $imageToDisplay; ?>" alt="文章图片" class="cover"/>