{php}
$temp=mt_rand(1,4);
$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/images/random/$temp.jpg";//需要在相应位置放置4张jpg的文件,名称为1,2,3,4
{/php}实现调用首张图片的代码:
<img src="{$temp}" />当文章没有图片的时候,随机调用主题style/images/random/下的1.jpg、2.jpg、3.jpg、4.jpg图片。
如果不想调用随机图片,可以把代码修成成如下:
{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/images/random/no-image.jpg";//在相应位置放置一张命名为no-image的jpg图片
{/php}用于分类文章列表下:
{foreach GetList(12,1,null,null,null,null,array('has_subcate' => ture)) as $related}
<li>
{php}
$pattern="/<[img|IMG].*?src=[\'|\"](.*?(?:[\.gif|\.jpg|\.png]))[\'|\"].*?[\/]?>/";
$content = $related->Content;
preg_match_all($pattern,$content,$matchContent);
if($matchContent[1][0])
$temp=$matchContent[1][0];
else
$temp="$host"."zb_users/theme/$theme/style/images/no-image.jpg";
{/php}
<a href="{$related.Url}" title="{$related.Title}"><img src="{$temp}" alt="{$related.Title}" /></a>
</li>
{/foreach}以上代码函数实现的是调用文章首张图片,如果文章没有图片就调用默认图片no-image.jpg