diff --git a/functions.php b/functions.php index b633d78..902420f 100644 --- a/functions.php +++ b/functions.php @@ -171,14 +171,24 @@ function allwords() { elseif ($chars>1000000){ echo '全站共 '.$chars.' 字,已写一本列夫·托尔斯泰的《战争与和平》了!';} } -function show_first_image($content) { - preg_match_all('//i', $content, $matches); - // 检查是否找到了图片 - if(count($matches) > 0 && count($matches[0]) > 0 ){ - return $matches[1][0]; +function img_postthumb($cid) { + $db = Typecho_Db::get(); + $rs = $db->fetchRow($db->select('table.contents.text') + ->from('table.contents') + ->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 { - return false; // 没有找到图片,返回 false -} + return ""; // 没有匹配到图片URL,返回空字符串 + } } //开始增加某些奇怪的东西 // 获取月份 diff --git a/post.php b/post.php index d2e2d4f..d6c41e3 100644 --- a/post.php +++ b/post.php @@ -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" /> - 阅读 + commentsNum('评论', '1 评论', '%d 评论'); ?> + commentsNum('0', '1', '%d '); ?> user->hasLogin() && $this->user->pass('editor', true)): ?> - - 编辑文章 + Edit

title() ?>

diff --git a/postlist.php b/postlist.php index 4e949be..f05117a 100644 --- a/postlist.php +++ b/postlist.php @@ -57,16 +57,12 @@ content); -// 检查自定义字段cover是否已设置 -$cover = $this->fields->cover; -// 如果自定义字段cover已设置,则优先使用 -// 如果没有设置,则显示文章的第一张图片 -$imageToDisplay = !empty($cover) ? $cover : $firstImage; -if($imageToDisplay): ?> + $firstImage = img_postthumb($this->cid); + $cover = $this->fields->cover; + $imageToDisplay = !empty($cover) ? $cover : $firstImage; + if($imageToDisplay): ?> 文章图片 - +