今天在群里有人想实现侧边栏最新文章获取图片,自己折腾了一阵子,成功解决了这个问题。为了以后再用得着,所以在这儿保存记录。当然除了调出图片,还可以调用当前分类、摘要等,具体看如下代码:首页在module.php中找到最新文章那段代码,直接用以下代码替换即可:- <?php //widget:最新文章
. `0 u; w3 l. U0 n - function widget_newlog($title){( ]8 E* z9 G' _" {5 ]* Y
- $index_newlognum = Option::get('index_newlognum');?>0 n# V( s& Y. m6 ?3 |7 F9 }
- <div class="cbl-one"><div class="title"><p><?php echo $title;?></p></div>
+ I" Q& I) t5 u& M) n6 l - <?php
/ g; h- a; b2 J) v7 L - $db = MySql::getInstance();& j" Z3 P8 e. q: v( a
- $sql = $db->query ("SELECT * FROM ".DB_PREFIX."blog inner join ".DB_PREFIX."sort WHERE hide='n' AND type='blog' AND top='n' AND sortid=sid order by date DESC limit 0,$index_newlognum"); while($row = $db->fetch_array($sql)){ $logpost = !empty($row['excerpt']) ? $row['excerpt'] : ''.$row['content'].''; if (!empty($row['excerpt'])){preg_match_all("/\<img.*?src\="(.*?)"[^>]*>/i", $row['excerpt'], $match); if(empty($match[1][0])) {' L# C$ [& R0 z4 w) h4 ~
- preg_match_all("/\<img.*?src\="(.*?)"[^>]*>/i",$row['content'],$match);}}else{preg_match_all("/\<img.*?src\="(.*?)"[^>]*>/i", $row['content'],$match);}& ^5 h H c- f
- $img = isset($match[0][0]) ? $match[0][0]:'<img src="图片地址" />';//无图片时显示; Z2 g; F0 Z2 I) m
- $date = gmdate('Y年m月d日', $row['date']);
$ l+ G% s$ \; n3 x$ h+ ^2 Z0 _9 r7 N - $content = strip_tags($logpost,'');
$ I. Y9 V0 w$ q. ~; h - $content = mb_substr($content,0,100,'utf-8');//摘要字数修改本代码中的100这个即可; H: Z: V5 {6 u
- $comment = ($row['comnum'] != 0) ? ''.$row['comnum'].'' : '0'; $gid = $row['gid']; $tag = $db -> query("SELECT * FROM ".DB_PREFIX."tag WHERE gid LIKE '%,$gid,%'");?>
- G7 V2 L1 t+ w! D( g- u- r4 b - <a href="<?php echo Url::sort($row['sid']);?>" title="<?php echo $row['sortname'];?>" /><?php echo $row['sortname'];?></a><br />
# C% g8 i a; N r/ ^- d - <a href="<?php echo Url::log($row['gid']);?>" title="<?php echo $row['title'];?>" /><?php echo $row['title'];?></a><br />, \4 p) e* l; M. y
- <?php echo $img;?><br />$ T% Q( J: y6 p$ g( [; }
- <?php echo $content;?>! H: G/ i1 Q, Q* x* U* f, s
- <?php };?></div><?php }?>
复制代码 随便附带上热门文章的代码 30天内按点击量排序:- <?php //widget:热门文章# U, v! y6 P! y' Z0 s7 z! g1 }3 F
- function widget_hotlog($title){
`- p$ g& k9 j! N4 B" A" c - $index_hotlognum = Option::get('index_hotlognum');?>, A4 p D' e: w7 ]
- <div class="cbl-one"><div class="title"><p><?php echo $title;?></p></div>* _2 K1 M1 s0 \9 v
- <?php $db = MySql::getInstance();$db = MySql::getInstance();
8 ~) {2 Z% K8 g7 g0 h - $time = time();7 ?8 ]" y, P" w- S5 S, X
- $sql = $db->query ("SELECT * FROM ".DB_PREFIX."blog inner join ".DB_PREFIX."sort WHERE hide='n' AND type='blog' AND date > $time - 30*24*60*60 AND top='n' AND sortid=sid order by `views` DESC limit 0,$index_hotlognum");
* M$ ?. \- z7 r0 e3 w - while($row = $db->fetch_array($sql)){ $logpost = !empty($row['excerpt']) ? $row['excerpt'] :''.$row['content'].''; if (!empty($row['excerpt'])){preg_match_all("/\<img.*?src\="(.*?)"[^>]*>/i",$row['excerpt'],$match);if(empty($match[1][0])){. `, ]- w5 K$ d% [- o
- preg_match_all("/\<img.*?src\="(.*?)"[^>]*>/i",$row['content'],$match);}}else{preg_match_all("/\<img.*?src\="(.*?)"[^>]*>/i", $row['content'],$match);}
5 w% r* l1 {9 a, J3 l4 i - $img = isset($match[0][0]) ? $match[0][0]:'<img src="图片地址" />';//无图片时显示; h) K* `" }# c* d- i4 x, W1 }
- $date = gmdate('Y年m月d日', $row['date']);- |9 L$ I0 b2 h$ D. ?, z
- $content = strip_tags($logpost,'');
0 a x( p$ M3 A1 Z2 A- H2 ^! N - $content = mb_substr($content,0,100,'utf-8');//摘要字数修改本代码中的100这个即可' C4 ~) |+ l5 l: {( s) }4 |
- $comment = ($row['comnum'] != 0) ? ''.$row['comnum'].'' : '0';" H! G* C8 V1 M+ @" e E
- $gid = $row['gid'];?>
# M$ u2 v% p( k% W4 v3 { - <li><a href="<?php echo Url::log($row['gid']);?>" title="<?php echo $row['title'];?>" /><?php echo $row['title'];?></a></li>- B5 R& K2 W& `0 u% `' I3 Z
- <?php };?></div><?php }?>
复制代码 代码说明:(CSS本人就不折腾了,自己捣鼓吧,如果你这个也不会的话,就不要折腾了)
: k" h! b% c. r% C; V所属分类链接:<?php echo Url::sort($row['sid']);?>/ g# v6 o5 o8 Q! d
所属分类标题:<?php echo $row['sortname'];?>/ q2 l" h+ h2 e5 c, m) S- d# D
文章链接:<?php echo Url::log($row['gid']);?>
- b8 L" d+ a1 y# _0 A文章标题:<?php echo $row['title'];?>" S7 F6 [; Y: }9 f0 u
当前第一张图片:<?php echo $img;?> ` ^& O8 o' _$ P6 t5 I% p0 i) b
当前文章摘要:<?php echo $content;?>
2 l, |& l4 B. d6 A当前评论:<?php echo $row['comnum'];?>5 e7 w8 C4 }6 Y5 v8 `
当前浏览量:<?php echo $row['views'];?>. ^0 @* [7 f& i, s$ ~* s
当前时间:<?php echo gmdate('Y年m月d日', $row['date']);?>
" n8 @' ^$ {4 @本栏目标题(侧边栏):<?php echo $title;?>% |2 A0 t3 {# L, Y) |+ z
|