几乎每一个zblogphp主题都需要用到调用文章图片的情况,特别是zblogCMS主题、企业主题,甚至大部分zblog博客主题也是需要直接调用文章中的第一张图片。
下面分享一个比较傻瓜式的省力办法,不需要在模板中写一大串代码的方法:
function 模板ID_thumbnail($related) {
global $zbp;
$temp=mt_rand(1,10);
$pattern="/<[img|IMG].*?src=[\'|\"](.*?(?:[\.gif|\.jpg|\.png]))[\'|\"].*?[\/]?>/";
$content = $related->Content;
preg_match_all($pattern,$content,$matchContent);
if(isset($matchContent[1][0])){
$thumb=$matchContent[1][0];
}else{
$thumb=$zbp->host . "zb_users/theme/" .$zbp->theme. "/include/random/" .$temp. ".jpg";
}
return $thumb;
}
注:在无图的时候随机调用了include/random/下的10张JPG图片,路径可自行修改,10这个数字也可改,模板ID一定是自己的模板应用ID即可。
下面直接说调用方法:
如果是博客文章列表,则直接在post-multi.php、post-istap.php里用
{模板_thumbnail($article)}
作为图片链接即可,如:<img src="{模板_thumbnail($article)}" alt="">
其它地方调用也是直接用,但$article需要替换!