Emlog实现调用分类下第一篇文章图片函数分享
2 d" J g% t6 B, C8 `; j效果图大家先看看
( k m$ W2 S, D- U
/ C) g+ H/ n$ }+ e. z的在模板文件下的module.php下添加如下函数- <?php //分类文章调用
, m6 H. `4 w7 e/ K- v# E9 L - function sort_log($sortid){( j7 T& w5 f3 |2 D" G) |$ ~7 f# ]
- $db = MySql::getInstance();
, p6 |) k b+ ^4 r! J( @ - $sql = $db->query ("SELECT * FROM ".DB_PREFIX."blog WHERE sortid='$sortid' AND type='blog' AND hide='n' order by date DESC limit 0,1");) P" W2 D; |) e- P2 r* e9 M- J
- $row = $db->fetch_array($sql);
4 }+ U: r5 f* g# r0 \$ J - if (!empty($row['excerpt'])){. b. h& N% _* ^2 s X+ r0 R
- preg_match_all("/\<img.*?src\="(.*?)"[^>]*>/i", $row['excerpt'], $match);
( K, W4 K, @3 t3 h/ H - if(empty($match[0][0]))6 D8 {" R) t3 k- x
- {preg_match_all("/\<img.*?src\="(.*?)"[^>]*>/i", $row['content'], $match);}; b6 m5 Z; h$ Y; N! \9 u* z
- }else{preg_match_all("/\<img.*?src\="(.*?)"[^>]*>/i", $row['content'], $match);}$ Z) a$ d) f% p) ?, U% v
- $logpost = !empty($row['excerpt']) ? $row['excerpt'] : ''.$row['content'].'';' z. i2 k$ u& h$ s; o& A \
- $num = rand(1,$imgnum);
2 a$ m+ D7 a+ b% e5 k4 A - $img = isset($match[0][0]) ? $match[0][0] : '<img src="'.TEMPLATE_URL.'images/shuyong_net/'.$num.'.jpg">';
y. ], p2 F/ R" F' n7 M - $content = strip_TAGs($logpost,'');
& j. z3 f" j2 A6 j2 I' t - $content = mb_substr($content,0,68,'utf-8');//
8 V! n6 P ^0 _2 ~% M0 C# k - $out .='<div class="sort-tw"><div class="sort-img"><a href="'.Url::log($row['gid']).'" title="'.$row['title'].'" >'.$img.'</a></div>
. }$ j" y1 L, @6 {: L8 f - <div class="sort-tt"><a href="'.Url::log($row['gid']).'" title="'.$row['title'].'" >'.$row['title'].'</a></div><div class="sort-nr">'.$content.'...</div></div>
8 x: n H$ H% l* W) ~ d - ';0 z& D8 h' ]+ l
- $sort_log_num = $sort_log_num -1;
; v0 V. \# P- z' W - $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为数量
) L# s8 ~2 p( S! l$ [, ` E R - while ($trow = $db->fetch_array($logs)){
/ z u8 R7 G! M# q$ D5 s7 t - $date = gmdate('m-d', $trow['date']);
9 N' ^5 W) K7 f5 t6 a - $trow['title'] = mb_substr($trow['title'],0,180,'utf-8');
6 v! r- m# f( }# A - $out .='<li><p><a href="'.Url::log($trow['gid']).'" title="'.$trow['title'].'">'.$trow['title'].'</a></p><span>'.$date.'</span></li>7 w0 H6 T2 v6 @ J
- ';
( }2 w b$ c+ H) S' y - }echo $out;}?>
复制代码 Emlog模板文件log_list.php下使用如下代码
* t; H' b" K; V2 N$ a e# b3 l' [- <span style="color: rgb(51, 51, 51); font-family: 微软雅黑, 宋体, sans-serif; line-height: 25px;"><?php global $CACHE;$sort_cache = $CACHE->readCache('sort'); ?></span>
w7 Y; Y5 \: H- S* R: h- R7 S - <span style="color: rgb(51, 51, 51); font-family: 微软雅黑, 宋体, sans-serif; line-height: 25px;"><?php echo $sort_cache[1]['sortname'];?>调用分类名称,其中[1]代表分类id</span>8 z3 l3 G; H7 u P
- <span style="color: rgb(51, 51, 51); font-family: 微软雅黑, 宋体, sans-serif; line-height: 25px;"><?php sort_log(1); ?></span>
复制代码
8 M0 R4 f* i% T6 E最后写上CSS就ok,css文件自己折腾吧0 e+ t- `6 s) r
|