在写zblog主题的时候,往往需要图文结合,特别是分类、右侧栏列表中调用文章时,此时应该怎么写呢?

1、需要现在include.php中填写入下代码,大意是从文章中查找img,也就是有没有图片,然后进行判断,当文章代码中含有img标签时,也就是有图的时候,提取文章的第一张图片链接,当没有图的时候,又分两种情况,主题配置中,加入$zbp->Config('yd1013')->noimgstyle开关,同时自定义一张无图调取指定图片“$zbp->Config('yd1013')->noimg”,当没有开启这个指定调用图片时,自动获取/include/random/路径中的jpg图片,从10个图片中提取一张显示。
//无需剪裁的图片调用// 当图片存在: {if $article->ImageCount >= 1}。。。{/if}// 当图片存在: {if !empty(yd1013_thumbnail($article))}。。。。。{/if}function yd1013_thumbnail($related) { global $zbp;
$temp = mt_rand(1, 10); $pattern = "/<[img|IMG].*?src=[\'|\"](.*?(?:[\.gif|\.jpg|\.png]))[\'|\"].*?[\/]?>/"; $content = $related->Content;
preg_match_all($pattern, $content, $matchContent); if (isset($related->Metas->postpic)) { $thumb = $related->Metas->postpic; } elseif (isset($matchContent[1][0])) { $thumb = $matchContent[1][0]; } else { if ($zbp->Config('yd1013')->noimgstyle == '1') { $thumb = $zbp->Config('yd1013')->noimg; }else { $thumb=$zbp->host . "zb_users/theme/" .$zbp->theme. "/include/random/" .$temp. ".jpg"; } } return $thumb;}注意,以上代码中,还有一个“$related->Metas->postpic”,这是在编辑器中加入的自定义缩略图上传功能,也就是上传了缩略图则直接调用这个缩略图,不管文章中有没有图片。
2、在主题模板中,需要调用图片的地方,加入:
{yd1013_thumbnail($article)}