68 lines
1.7 KiB
PHP
68 lines
1.7 KiB
PHP
<?php
|
||
if (!defined('__TYPECHO_ROOT_DIR__')) exit;
|
||
|
||
/**
|
||
* 获取文章缩略图
|
||
* 获取顺序: 自定义字段(thumb)> 文章第一张图片 > 系统图库
|
||
* @param $obj
|
||
* @return 获取文章头图
|
||
*/
|
||
function get_postthumb($obj) {
|
||
|
||
if(isset($obj->fields->thumb)){
|
||
return $obj->fields->thumb;
|
||
}
|
||
|
||
preg_match( "/<img[^>]+src\s*=\s*['\"]([^'\"]+)['\"][^>]*>/ ", $obj->content, $matches );
|
||
if(isset($matches[1])){
|
||
return $matches[1];
|
||
}
|
||
|
||
preg_match( "/!\[(.*?)\]\((.*?)\)/ ", $obj->content, $matches );
|
||
if(isset($matches[2])) {
|
||
return$matches[2];
|
||
}
|
||
|
||
$thumbPath = __DIR__ .'/static/medias/featureimages/';
|
||
$files = scandir($thumbPath);
|
||
$thumbs = [];
|
||
for ($i = 0; $i < count($files); $i++) {
|
||
if(is_file($thumbPath . $files[$i])){
|
||
$thumbs[] = $files[$i];
|
||
}
|
||
}
|
||
|
||
return Helper::options()->themeUrl .'/static/medias/featureimages/'. $thumbs[rand(0, count($thumbs) - 1)];
|
||
}
|
||
|
||
/**
|
||
* @param $obj 对象
|
||
* @return string 上一页url
|
||
*/
|
||
function getPrevPageLink($obj){
|
||
ob_start();
|
||
$obj->pagelink("prev", "prev");
|
||
$pageLink = ob_get_contents();
|
||
ob_end_clean();
|
||
preg_match("/<a[^>]+href\s*=\s*['\"]([^'\"]+)['\"][^>]*>/", $pageLink, $matches);
|
||
if(isset($matches[1])){
|
||
return $matches[1];
|
||
}
|
||
return "#";
|
||
}
|
||
|
||
/**
|
||
* @param $obj 对象
|
||
* @return string 下一页url
|
||
*/
|
||
function getNextPageLink($obj){
|
||
ob_start();
|
||
$obj->pageLink("next", "next");
|
||
$pageLink = ob_get_contents();
|
||
ob_end_clean();
|
||
preg_match("/<a[^>]+href\s*=\s*['\"]([^'\"]+)['\"][^>]*>/", $pageLink, $matches);
|
||
if(isset($matches[1])){
|
||
return $matches[1];
|
||
}
|
||
return "#";
|
||
} |