typecho缩略图函数分享
生活随笔
收集整理的這篇文章主要介紹了
typecho缩略图函数分享
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在主題下functions.php文件中加入以下代碼:
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)
->order('table.contents.cid', Typecho_Db::SORT_ASC)
->limit(1));
$img = unserialize($rs['text']);
if (empty($img)){
echo $imgurl;
}
else{
echo '你的博客地址'.$img['path'];
}
}
模板處調用:
<?php echo thumb($this->cid); ?>
效果:如果有圖片附件,就調用第1個圖片附件,否則隨機顯示。
總結
以上是生活随笔為你收集整理的typecho缩略图函数分享的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Typecho伪静态规则、301跳转
- 下一篇: Typecho 自定义分页样式