在开发ZBlog主题中,文章列表往往需要使用缩略图,我们可以自动调用文章中第一张图片作为缩略图,如果文章中没有图片的话 那么我们就使用主题自带的5中图片随机显示一张。
代码如下:
{php} $temp=mt_rand(1,5); $pattern="/<[img|IMG].*?src=[\'|\"](.*?(?:[\.gif|\.jpg|\.png]))[\'|\"].*?[\/]?>/"; $content = $article->Content; preg_match_all($pattern,$content,$matchContent); if(isset($matchContent[1][0])) $temp=$matchContent[1][0]; else $temp=$zbp->host."zb_users/theme/$theme/style/img/$temp.jpg"; //需要在相应位置放置5张jpg的文件,名称为1,2,3,4,5 {/php}
调用方法:
<img src="{$temp}" />