Emlog教程 - 侧边栏最新文章获取图片、分类、摘要等-站长杂谈下载

Emlog教程 - 侧边栏最新文章获取图片、分类、摘要等

来自版块: 站长杂谈发表于: 2015-8-17 12:03:05
34559
0
如本资源下载地址失效,请点击此处进行反馈
开通本站Svip会员,全站资源免费下
今天在群里有人想实现侧边栏最新文章获取图片,自己折腾了一阵子,成功解决了这个问题。为了以后再用得着,所以在这儿保存记录。当然除了调出图片,还可以调用当前分类、摘要等,具体看如下代码:首页在module.php中找到最新文章那段代码,直接用以下代码替换即可:
  1. <?php //widget:最新文章
    . `0 u; w3 l. U0 n
  2. function widget_newlog($title){( ]8 E* z9 G' _" {5 ]* Y
  3. $index_newlognum = Option::get('index_newlognum');?>0 n# V( s& Y. m6 ?3 |7 F9 }
  4. <div class="cbl-one"><div class="title"><p><?php echo $title;?></p></div>
    + I" Q& I) t5 u& M) n6 l
  5. <?php
    / g; h- a; b2 J) v7 L
  6. $db = MySql::getInstance();& j" Z3 P8 e. q: v( a
  7. $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 ~
  8. preg_match_all("/\<img.*?src\="(.*?)"[^>]*>/i",$row['content'],$match);}}else{preg_match_all("/\<img.*?src\="(.*?)"[^>]*>/i", $row['content'],$match);}& ^5 h  H  c- f
  9. $img = isset($match[0][0]) ? $match[0][0]:'<img src="图片地址" />';//无图片时显示; Z2 g; F0 Z2 I) m
  10. $date = gmdate('Y年m月d日', $row['date']);
    $ l+ G% s$ \; n3 x$ h+ ^2 Z0 _9 r7 N
  11. $content = strip_tags($logpost,'');
    $ I. Y9 V0 w$ q. ~; h
  12. $content = mb_substr($content,0,100,'utf-8');//摘要字数修改本代码中的100这个即可; H: Z: V5 {6 u
  13. $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
  14. <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
  15. <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
  16. <?php echo $img;?><br />$ T% Q( J: y6 p$ g( [; }
  17. <?php echo $content;?>! H: G/ i1 Q, Q* x* U* f, s
  18. <?php };?></div><?php }?>
复制代码

随便附带上热门文章的代码 30天内按点击量排序:
  1. <?php //widget:热门文章# U, v! y6 P! y' Z0 s7 z! g1 }3 F
  2. function widget_hotlog($title){
      `- p$ g& k9 j! N4 B" A" c
  3. $index_hotlognum = Option::get('index_hotlognum');?>, A4 p  D' e: w7 ]
  4. <div class="cbl-one"><div class="title"><p><?php echo $title;?></p></div>* _2 K1 M1 s0 \9 v
  5. <?php $db = MySql::getInstance();$db = MySql::getInstance();
    8 ~) {2 Z% K8 g7 g0 h
  6. $time = time();7 ?8 ]" y, P" w- S5 S, X
  7. $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
  8. 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
  9. 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
  10. $img = isset($match[0][0]) ? $match[0][0]:'<img src="图片地址" />';//无图片时显示; h) K* `" }# c* d- i4 x, W1 }
  11. $date = gmdate('Y年m月d日', $row['date']);- |9 L$ I0 b2 h$ D. ?, z
  12. $content = strip_tags($logpost,'');
    0 a  x( p$ M3 A1 Z2 A- H2 ^! N
  13. $content = mb_substr($content,0,100,'utf-8');//摘要字数修改本代码中的100这个即可' C4 ~) |+ l5 l: {( s) }4 |
  14. $comment = ($row['comnum'] != 0) ? ''.$row['comnum'].'' : '0';" H! G* C8 V1 M+ @" e  E
  15. $gid = $row['gid'];?>
    # M$ u2 v% p( k% W4 v3 {
  16. <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
  17. <?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

全部评论 0

您需要登录后才可以回帖 立即登录
登录
0
0
0
返回顶部