Emlog实现调用分类下第一篇文章图片函数分享
效果图大家先看看
的在模板文件下的module.php下添加如下函数
代码语言:javascript复制<?php //分类文章调用
function sort_log($sortid){
$db = MySql::getInstance();
$sql = $db->query ("SELECT * FROM ".DB_PREFIX."blog WHERE sortid='$sortid' AND type='blog' AND hide='n' order by date DESC limit 0,1");
$row = $db->fetch_array($sql);
if (!empty($row['excerpt'])){
preg_match_all("/<img.*?src="(.*?)"[^>]*>/i", $row['excerpt'], $match);
if(empty($match[0][0]))
{preg_match_all("/<img.*?src="(.*?)"[^>]*>/i", $row['content'], $match);}
}else{preg_match_all("/<img.*?src="(.*?)"[^>]*>/i", $row['content'], $match);}
$logpost = !empty($row['excerpt']) ? $row['excerpt'] : ''.$row['content'].'';
$num = rand(1,$imgnum);
$img = isset($match[0][0]) ? $match[0][0] : '<img src="'.TEMPLATE_URL.'images/shuyong_net/'.$num.'.jpg">';
$content = strip_tags($logpost,'');
$content = mb_substr($content,0,68,'utf-8');//
$out .='<div class="sort-tw"><div class="sort-img"><a href="'.Url::log($row['gid']).'" title="'.$row['title'].'" >'.$img.'</a></div>
<div class="sort-tt"><a href="'.Url::log($row['gid']).'" title="'.$row['title'].'" >'.$row['title'].'</a></div><div class="sort-nr">'.$content.'...</div></div>
';
$sort_log_num = $sort_log_num -1;
$logs = $db->query ("SELECT * FROM ".DB_PREFIX."blog WHERE sortid='$sortid' AND type='blog' AND hide='n' order by date DESC limit 1,10");//1表示从第2篇文章开始,10为数量
while ($trow = $db->fetch_array($logs)){
$date = gmdate('m-d', $trow['date']);
$trow['title'] = mb_substr($trow['title'],0,180,'utf-8');
$out .='<li><p><a href="'.Url::log($trow['gid']).'" title="'.$trow['title'].'">'.$trow['title'].'</a></p><span>'.$date.'</span></li>
';
}echo $out;}?>
Emlog模板文件log_list.php下使用如下代码
代码语言:javascript复制<?php global
<?php echo $sort_cache[1]['sortname'];?>调用分类名称,其中[1]代表分类id
<?php sort_log(1); ?>
最后写上css就ok,css文件自己折腾吧
分类图文显示
代码语言:javascript复制<?php //分类图文
function home_cp(){
$ysort = array(1,2,3);//分类id
$num = 10;//显示数量
$db = MySql::getInstance();global $CACHE; $sort_cache = $CACHE->readCache('sort');foreach($ysort as $key => $i){$key = $key 1;?>
<div class="side product">
<div class="title"><p><?php echo $sort_cache[$i]['sortname'];?></p><a href="<?php echo Url::sort($i);?>" title="<?php echo $sort_cache[$i]['sortname'];?>"><span></span></a></div>
<?php $logs = $db->query("SELECT * FROM ".DB_PREFIX."blog WHERE sortid='$i' AND type='blog' AND hide='n' order by date DESC limit 0,$num");
while($value = $db->fetch_array($logs)){$img_url = TEMPLATE_URL.'images/news.jpg';if(img_fj($value['gid'])){$img_url = img_fj($value['gid']);}elseif(img_zw($value['content'])){$img_url = img_zw($value['content']);}else{$img_url;}?>
<ul class="zx"><li><a href="<?php echo Url::log($value['gid']);?>"><img src="<?php echo $img_url;?>" /><p><?php echo $value['title'];?></p></a></li></ul><?php }?></div><?php }}?>