李涛自留地
回到顶部
首页 > 建站 > ZblogPhp主题制作学习系列四 ZblogPhp调用文章缩略图的方法

ZblogPhp主题制作学习系列四 ZblogPhp调用文章缩略图的方法

ZblogPhp主题制作学习系列四,如何调用文章缩略图?ZblogPhp调用文章缩略图的方法有哪些?在网站首页、分类页、文章页、Tags等其他页面,都会用到调用文章的缩略图,这样网站看起来也更加美观。那么ZblogPhp主题制作中,如何调用文章缩略图呢?

在默认主题的首页中,是没有涉及到调用文章缩略图调用的,但在Zblog的官网有介绍如何调用文章缩略图的方法:

{php}
$temp=mt_rand(1,4);
$pattern="/<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}" /> //官方介绍的调用文章缩略图的调用标签。

在以上代码中,先是定义了一个1到4的变量,然后判断了如果文章没有缩略图的时候,就随机调用主题Style文件下的Images文件夹下的Random文件夹下的4张随机图片。这个地方的路径可根据自己的实际需求缩短路径,直接将图片放在Images路径下即可,感觉官方写的这个路径有点太长了。

$temp=$zbp->host."zb_users/theme/$theme/style/images/$temp.jpg";

以上的调用文章图片为缩略图的方法稍微有点麻烦,要在不同页面都调用文章缩略图的时候,就必须将调用代码都写一遍,这大大加大了代码量,这个时候我们可以将以上代码写入主题根目录的include.php中(如果没有这个php文件,可以自己新建一个),给以上代码定义一个方法,在不同类型的页面时直接调用此方法就可以了。

ZblogPhp主题制作学习系列四 ZblogPhp调用文章缩略图的方法
//缩略图调用
function lishiwo_thumbnail($related) {
    global $zbp;
 $temp=mt_rand(1,4);
 $pattern="/<[img|IMG].*?src=[\'|\"](.*?(?:[\.gif|\.jpg|\.png]))[\'|\"].*?[\/]?>/";
 $content = $related->Content;
 preg_match_all($pattern,$content,$matchContent);
 if(isset($matchContent[1][0])){
  $thumb=$matchContent[1][0];
 }else{
  $thumb=$zbp->host . "zb_users/theme/" .$zbp->theme. "/images/" .$temp. ".jpg";
 }
    return $thumb;
}

以上是我写入Include.php文件中的缩略图调用方法。以上就是今日学习的ZblogPhp主题制作如何调用文章缩略图的方法。

若非作者标注皆为李涛自留地原创文章,转载或复制请以超链接形式并注明出处。