关于ZBLOGPHP调用缩略图的代码有好多,有调用多张缩略图的,这里说的只调用一张缩略图的。

这里整理出三段较常用的代码。

一、调用缩略图,如没有缩略图显示指定图片:

{foreach $articles as $article}
{php}$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/aa.jpg";/*指定图片路径*/{/php}
<a href="{$article.Url}" target="_blank"><img src="{$temp}" /></a>{/foreach}

这段代码在文章有图时,会调用第一张图做为文章缩略图,如文章内容没有图片,则会显示指定路径下名字为"aa"的.jpg格式图片。

二、调用缩略图,如没有缩略图随机显示指定的几张图片:

这段代码就会显得生动些,在文章内容没有图片的时候,会在指定的图片中,随机显示一张,不会显得那么生硬。

<a href="{$article.Url}" target="_blank"> <img src="{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="$host/zb_users/theme/$theme/style/images/random/$temp.jpg";/*指定图片路径*/                               
{/php}{$temp}" alt="{$article.Title}" /></a>

这段代码在文章有图时,会调用第一张图做为文章缩略图,如文章内容没有图片,则会随机显示指定路径下名字为1-5的.jpg格式图片。

5张随机显示其中一张,5张图片要分别命名为:1.jpg、2.jpg、3.jpg、4.jpg、5.jpg。

三、直接用zblogphp的原生代码调用文章缩略图,代码如下:

<img src="{php}$pattern="/<[img|IMG].*?src=[\'|\"](.*?(?:[\.gif|\.jpg|\.png]))[\'|\"].*?[\/]?>/";
$content = $article->Content;preg_match_all($pattern,$content,$matchContent);echo $matchContent[1][0];
{/php}" />

这段代码只是调用缩略图,如果文章内容没有图片,则不显示缩略图,没图空着可能就不太好看。

四、文章不上传指缩略图,随机调用图片,代码如下:

	{if $article.Metas.thumburl}
<img src="{$article.Metas.thumburl}" alt="{$article.Title}">
{else}
{php}
$temp=mt_rand(1,10);$pattern="/<[img|IMG].*?src=[\'|\"](.*?(?:[\.gif|\.jpg|\.png]))[\'|\"].*?[\/]?>/";
$tempp=$zbp->host."zb_users/theme/$theme/random/$temp.jpg"{/php}
<img src="{$tempp}" alt="无缩略图">
{/if}

这段代码在文章不上传缩略图时,则会随机显示指定路径下名字为1-10的.jpg格式图片。