Typecho调用文章第一张图,第N张图以及无图显示随机图
有时候Typecho想调用文章第二张图,或者在输出多图的时候想对多图顺序的一个调用
可以尝试下面的方法
- function thumb($cid) {
- if (empty($imgurl)) {
- $rand_num = 10; //随机图片数量,根据图片目录中图片实际数量设置
- if ($rand_num == 0) {
- $imgurl = "随机图片存放目录/0.jpg";
- //如果$rand_num = 0,则显示默认图片,须命名为"0.jpg",注意是绝对地址
- }else{
- $imgurl = "随机图片存放目录/".rand(1,$rand_num).".jpg";
- //随机图片,须按"1.jpg","2.jpg","3.jpg"...的顺序命名,注意是绝对地址
- }
- }
- $db = Typecho_Db::get();
- $rs = $db->fetchRow($db->select('table.contents.text')
- ->from('table.contents')
- ->where('table.contents.type = ?', 'attachment')
- ->where('table.contents.parent= ?', $cid)
- ->where('table.contents.order= ?', 2)
- ->order('table.contents.cid', Typecho_Db::SORT_ASC)
- ->limit(1));
- $img = unserialize($rs['text']);
- if (empty($img)){
- echo $imgurl;
- }
- else{
- echo '你的博客地址'.$img['path'];
- }
- }
-
以上代码放入Functions.php 文件中。
然后在想在调用的文件里放入以下代码,这里我放入的是首页位置。
前端输出显示:<?php echo thumb($this->cid); ?>
上面的代码里面
- ->where('table.contents.order= ?', 2)
这里的2指定是第二个附件数据
如果控制输出第几个,或者多张图顺序的时候,从这里入手应该就可以了
代码参考:http://laolion.com/archives/698.html
第二张方法:
- function thumbside($con) {
- preg_match_all('/\<img.*?src\=\"(.*?)\"[^>]*>/i', $con, $thumbUrl); //通过正则式获取图片地址
- preg_match_all('/\!\[.*?\]\((http(s)?:\/\/.*?(jpg|png))/i', $con, $patternMD); //通过正则式获取图片地址
- preg_match_all('/\[.*?\]:\s*(http(s)?:\/\/.*?(jpg|png))/i', $con, $patternMDfoot);
- if(count($thumbUrl[0])>1){
- return $thumbUrl[1][1]; //当找到一个src地址的时候,输出缩略图
- }else if(count($patternMD[0])>1){
- return $patternMD[1][1];
- }else if(count($patternMDfoot[0])>1){
- return $patternMDfoot[1][1];
- }else{
-
- $rand_num = 5;
- return Helper::options()->siteUrl."usr/themes/pure/randimg/".rand(1,$rand_num).".jpg";
- }
- }
文章由官网发布,如若转载,请注明出处:https://www.veimoz.com/895
0 评论
3.5k
发表评论
热门文章
自媒体博客Spimes主题45w 阅读
Spimes主题专为博客、自媒体、资讯类的网站设计....
Splity博客双栏主题15w 阅读
仿制主题,Typecho博客主题,昼夜双版设计,可....
vCard主题个人简历主题13w 阅读
一款个人简历主题,可以简单搭建一下,具体也比较简单....
Spzac个人资讯下载类主题12w 阅读
用于作品展示、资源下载,行业垂直性网站、个人博客,....
热评文章
自媒体博客Spimes主题424 评论
Splity博客双栏主题191 评论
Spzac个人资讯下载类主题89 评论
Splinx博客图片主题35 评论
Spzhi知识付费社区主题34 评论
三栏清新博客S_blog主题31 评论
vCard主题个人简历主题29 评论
Pure轻简主题29 评论
chenyu
4月7日
能不能支持deepseek