温馨提示:所有代码请在模板默认上测试,因为不确定模板是否有相同代码出现,所以在运行上可能会出错。 第一种方法:模板的module.php文件中放入如下代码:
代码语言:javascript复制<?php //调用分类文章,图文显示
function sheli_tw($sort, $num){$db = MySql::getInstance();
$sql = "SELECT gid,title,date,content,sortid,views,comnum FROM ".DB_PREFIX."blog WHERE sortid=".$sort." AND hide='n' ORDER BY `date` DESC LIMIT 0,$num";
$go = $db->query($sql);while($row = $db->fetch_array($go)){
$img_url = TEMPLATE_URL.'images/sheli.jpg';//无图片时显示
if(pic_thumb($row['content'])){$img_url = pic_thumb($row['content']);//查找正文第一张图片
}elseif(picthumb($row['gid'])){$img_url = picthumb($row['gid']);//查找附件第一张图片
}else{$img_url;}?>
<li><p><a href="<?php echo Url::log($row['gid']);?>"><img src="<?php echo $img_url;?>" /></p><span><?php echo $row['title'];?></a></span></li>
<?php }}function picthumb($blogid) {$db = MySql::getInstance();$sql = "SELECT * FROM ".DB_PREFIX."attachment WHERE blogid=".$blogid." AND (`filepath` LIKE '%jpg' OR `filepath` LIKE '%gif' OR `filepath` LIKE '%png') ORDER BY `aid` ASC LIMIT 0,1";$imgs = $db->query($sql);while($row = $db->fetch_array($imgs)){$pict.= ''.BLOG_URL.substr($row['filepath'],3,strlen($row['filepath'])).'';}return $pict;}function pic_thumb($content){preg_match_all("/<img.*?src="(.*?)"[^>]*>/i", $content, $img);$imgsrc = !empty($img[1]) ? $img[1][0] : '';if($imgsrc):return $imgsrc;endif;}?>
调用方式:<?php sheli_tw(1,10);?>;其中1表示分类id,10表示数量
第二种方法:在模板的module.php文件中放入如下代码:
代码语言:javascript复制<?php //全局匹配正文中的图片并存入imgsrc中
function img_zw($content){preg_match_all("|<img[^>] src="([^>"] )"?[^>]*>|is", $content, $img);$imgsrc = !empty($img[1]) ? $img[1][0] : '';if($imgsrc):return $imgsrc;endif;}
//Custom: 获取附件第一张图片
function img_fj($logid){$db = MySql::getInstance();$sql = "SELECT * FROM ".DB_PREFIX."attachment WHERE blogid=".$logid." AND (`filepath` LIKE '%jpg' OR `filepath` LIKE '%gif' OR `filepath` LIKE '%png') ORDER BY `aid` ASC LIMIT 0,1";$imgs = $db->query($sql);$img_path = "";while($row = $db->fetch_array($imgs)){$img_path .= BLOG_URL.substr($row['filepath'],3,strlen($row['filepath']));}
return $img_path;}?>
然后在需要显示的位置放入下面代码
代码语言:javascript复制<?php
$db = MySql::getInstance();
$sql = "SELECT gid,title,date,content,sortid,views,comnum FROM ".DB_PREFIX."blog WHERE type='blog' and sortid='1' and hide='n' ORDER BY `date` DESC LIMIT 0,15";
$go = $db->query($sql);while($value = $db->fetch_array($go)){
$img_url='';if(img_zw($value['content'])){$img_url = img_zw($value['content']);}elseif(img_fj($value['gid'])){$img_url = img_fj($value['gid']);}else{$img_url;}
$gid=$value['gid'];
global $CACHE;$log_cache_tags = $CACHE->readCache('logtags');
if (!empty($log_cache_tags[$gid])){$tag = '标签:';foreach ($log_cache_tags[$gid] as $sl){$tag .= "<a href="".Url::tag($sl['tagurl'])."">".$sl['tagname'].'</a> ';}}
?>
图片:<img src="<?php echo $img_url;?>" />
日期:<?php echo gmdate('Y-m-d',$value['date']);?>
标签:<?php echo $tag;?>
标题:<a href="<?php echo Url::log($value['gid']);?>" title="<?php echo $value['title'];?>"><?php echo $value['title'];?></a>
描述:<?php echo subString(strip_tags(str_replace(array("n"," "," "),"",$value['content'])),0,80);?>
<?php }?>
sortid='1'中的1为分类ID,LIMIT 0,15中的15为显示数量