Skip to content

Commit

Permalink
自动选择第一张图片为头图
Browse files Browse the repository at this point in the history
  • Loading branch information
youranreus committed May 14, 2021
1 parent 9da7410 commit 1bb8f00
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
23 changes: 23 additions & 0 deletions functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ function themeConfig($form) {
$defaultPostIMG = new Typecho_Widget_Helper_Form_Element_Text('defaultPostIMG', NULL, NULL, _t('没有设置文章头图的就用这里的图片啦') , _t('http://...'));
$form->addInput($defaultPostIMG);

$enableFirstIMG = new Typecho_Widget_Helper_Form_Element_Radio('enableFirstIMG', array(
'1' => _t('cool') ,
'0' => _t('nope')
) , '1', _t('是否使用文章第一张图片作为文章首图') , _t('当没有设置头图时<br/>默认为开启<br/>p.s 不会选择[photo]短代码中的图片'));
$form->addInput($enableFirstIMG);

$headerLOGO = new Typecho_Widget_Helper_Form_Element_Text('headerLOGO', NULL, NULL, _t('头部logo') , _t('如果留空则不显示'));
$form->addInput($headerLOGO);

Expand Down Expand Up @@ -754,3 +760,20 @@ function Links($sorts = NULL) {
}
echo $link ? $link : '<li>暂无链接</li>';
}

/**
* 获取文章首图
*/
function getPostImg($archive) {
$img = array();
// 匹配 img 的 src 的正则表达式
preg_match_all("/<img.*?src=\"(.*?)\".*?\/?>/i", $archive->content, $img);
// 判断是否匹配到图片
if (count($img) > 0 && count($img[0]) > 0) {
// 返回图片
return $img[1][0];
} else {
// 如果没有匹配到就返回 false
return false;
}
}
2 changes: 1 addition & 1 deletion index.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*
* @package G
* @author 季悠然
* @version 2.4.2
* @version 2.4.3
* @link https://gundam.exia.xyz/
*/

Expand Down
2 changes: 1 addition & 1 deletion post.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
?>

<div id="post">
<div id="post-header" style="background-image:url(<?php $imgurl = $this->fields->imgurl;if($imgurl != ''){echo $imgurl;}else{$this->options->defaultPostIMG();}?>)">
<div id="post-header" style="background-image:url(<?php $imgurl = $this->fields->imgurl;if($imgurl != ''){echo $imgurl;}else{if($this->options->enableFirstIMG == 1 && getPostImg($this)){echo getPostImg($this);}else{$this->options->defaultPostIMG();}}?>)">
<div id="post-header-mask">
<div id="post-header-content">
<h2 id="post-content-title"><?php $this->title();?></h2>
Expand Down

0 comments on commit 1bb8f00

Please sign in to comment.