Loading... 最近入手了**友人c**的[handsome][2]主题,就是现在博客用的这个主题。 发现后台外观设置里没有自定义头图为自己随机图片API的选项,在网上搜索半天,发现大部分教程都是上传图片到js目录再调用,但是这样图片走的就是自己服务器宽带,对于小水管的建站机子不友好。 之前搭建自定义图片API时准备了一堆图片直链,现在可以派上用场了。 打开主题目录,找到<span style="color:#FF9900">handsome/libs/Content.php</span> 搜索<span style="color:#FF9900">$random = STATIC_PATH</span> 原部分代码应该为 ``` public static function whenSwitchHeaderImgSrc($widget, $index = 0, $howToThumb, $attach, $content, $thumbField) { $randomNum = unserialize(INDEX_IMAGE_ARRAY); // 随机缩略图路径 $random = STATIC_PATH . 'img/sj/' . @$randomNum[$index] . '.jpg';//如果有文章置顶,这里可能会导致index not undefined ``` 修改后代码 ``` public static function whenSwitchHeaderImgSrc($widget, $index = 0, $howToThumb, $attach, $content, $thumbField) { $randomNum = unserialize(INDEX_IMAGE_ARRAY); // 随机缩略图路径 //$random = STATIC_PATH . 'img/sj/' . @$randomNum[$index] . '.jpg';//如果有文章置顶,这里可能会导致index not undefined //从文本获取链接 $filename = "/www/wwwroot/weinb.top/usr/themes/handsome/libs/pic.txt"; if(!file_exists($filename)){ die('文件不存在'); } //从文本获取链接 $pics = []; $fs = fopen($filename, "r"); while(!feof($fs)){ $line=trim(fgets($fs)); if($line!=''){ array_push($pics, $line); } } $random = $pics[array_rand($pics)]; ``` 代码中<span style="color:#FF9900">/www/wwwroot/weinb.top/usr/themes/handsome/libs/pic.txt</span>为你图片外链txt的绝对路径,请自行修改 TXT中的外链存放格式为一个图片链接占一行  保存文件测试显示效果 [1]: https://www.ihewro.com/ [2]: https://www.ihewro.com/archives/489/ 最后修改:2020 年 12 月 26 日 09 : 43 PM © 允许规范转载 赞赏 如果觉得我的文章对你有用,请随意赞赏 ×Close 赞赏作者 扫一扫支付 支付宝支付 微信支付
测试评论