typecho主题开发— typecho获取文章缩略图,随机图

typecho主题开发— typecho获取文章缩略图,随机图

小唐
2023-05-15 / 0 评论 / 173 阅读 / 正在检测是否收录...
温馨提示:
本文最后更新于2023年05月15日,已超过340天没有更新,若内容或图片失效,请留言反馈。

前言

用Brave勇敢爱主题很多年了,原开源作者是免费的,在作者看到有人美化了一下就开价52块钱(ps:比我之前买handsome的主题还要贵)还在博客说今年的520过后就涨价到99,看了下对方的源代码写的不是很好,感觉不值这个加钱,我还是自己重新改下在开源吧,等空的时候也把小程序也弄出来。

网上写获取文章缩略图的文章不是很多,这里记录一下今天在主题中添加的代码

function img_postthumb($thumbThis) {
    $db = Typecho_Db::get();
    $rs = $db->fetchRow($db->select('table.contents.text')
        ->from('table.contents')
        ->where('table.contents.cid=?', $thumbThis->cid)
        ->order('table.contents.cid', Typecho_Db::SORT_ASC)
        ->limit(1));
    preg_match_all('/\<img.*?src\=\"(.*?)\"[^>]*>/i', $rs['text'], $thumbUrl);  //通过正则式获取图片地址
    preg_match_all('/\!\[.*?\]\((http(s)?:\/\/.*?(jpg|png))/i', $rs['text'], $patternMD);  //通过正则式获取图片地址
    preg_match_all('/\[.*?\]:\s*(http(s)?:\/\/.*?(jpg|png))/i', $rs['text'], $patternMDfoot);  //通过正则式获取图片地址
    if(count($thumbUrl[0])>0){
        return $thumbUrl[1][0];  //当找到一个src地址的时候,输出缩略图
    }else if(count($patternMD[0])>0){
        return $patternMD[1][0];
    }else if(count($patternMDfoot[0])>0){
        return $patternMDfoot[1][0];
    }else{
        //在主题根目录下的 /img 目录下放随机图片 thumb_开头
        //如:thumb_1.jpg
        return $thumbThis->widget('Widget_Options')->themeUrl."/img/thumb_".rand(1,7).".jpg";
        
    }
}`

文件调用方式

<?php echo img_postthumb($this); ?>
0

评论

博主关闭了所有页面的评论,留言评论可通过微信关注公众号:得水小筑