php版的zblog提供了获取文章首张图片的函数代码,直接把函数代码添加到模板文件中,然后调用对应的变量输入图片url即可,默认用于最新文章列表,通过代码调整可用于分类文章列表,比如zblog php企业模板的首页产品展示列表。
zblog官方z-WiKi提供的代码:
{php}$temp=mt_rand(1,4);$pattern="//";$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}
调用代码:
以上代码默认调用文章首张图片,当文章没有图片的时候,随机调用主题style/images/random/下的1.jpg、2.jpg、3.jpg、4.jpg图片。如果不想调用随机图片,可以修改一下:
{php}$pattern="//";$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}
{php}
$pattern="//";
$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}
{/foreach}调用文章首张图片,如果文章没有图片就调用默认图片no-image.jpg