有时为了文章页面美观,我们会调用文章的首张图片或者随机主题图片进行展示,而新版的zblog官方提供了直接调用文章拖图片的函数代码,通过把这个函数代码天街到相应的模板文件,调用相应变量,输入图片url即可实现。其中zblog官方z-WiKi提供的代码如下:

   <pre><code>{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?imageMogr2/format/webp";//需要在相应位置放置4张jpg的文件,名称为1,2,3,4

   {/php}
   </code></pre> 

实现调用首张图片的代码:

       <pre><code><img src="{$temp}" />
       </code></pre>

当文章没有图片的时候,随机调用主题style/images/random/下的1.jpg、2.jpg、3.jpg、4.jpg图片。
如果不想调用随机图片,可以把代码修成成如下:

   <pre><code>{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?imageMogr2/format/webp";//在相应位置放置一张命名为no-image的jpg图片

   {/php}
   </code></pre> 

用于分类文章列表下:

   <pre><code>{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?imageMogr2/format/webp";

   {/php}

   <a href="{$related.Url}" title="{$related.Title}"><img src="{$temp}" alt="{$related.Title}" /></a>

   </li>

   {/foreach}
   </code></pre> 

以上代码函数实现的是调用文章首张图片,如果文章没有图片就调用默认图片no-image.jpg


扫描二维码,在手机上阅读!