Emlog非插件实现文章添加相关文章的方法
: w) a$ A, V: ?8 Q+ B/ @1 P第一步:在模板文件module.php中加入以下代码- <?php# G: ]; c# z8 M4 \
- //文章详情页下相关文章7 q) p& P! J9 f1 O5 \
- function xg_logs($logData = array()){
Y7 U; g9 N, s$ q" t - if (is_file($configfile)) {require $configfile;}else{
2 A4 s3 C& _# `* A$ |2 d# J - $related_log_type = 'sort';//相关日志类型,sort为分类,tag为标签;- I6 w9 X9 N( W$ B2 Q1 o& _1 K
- $related_log_sort = 'views_desc';//排列方式,views_desc 为点击数(降序)comnum_desc 为评论数(降序) rand 为随机 views_asc 为点击数(升序)comnum_asc 为评论数(升序)
2 k5 w4 W/ Y& P - $related_log_num = '10'; //显示文章数8 h7 h2 J; N1 @7 I' H, `
- $related_inrss = 'y'; //是否显示在rss订阅中,y为是,其它值为否
, U+ M* r9 w' o C - }global $value;$DB = MySql::getInstance();$CACHE = Cache::getInstance();extract($logData);if($value)0 w# {7 h) P; ]; `+ P
- {$logid = $value['id'];$sortid = $value['sortid'];global $abstract;}
8 o! Z8 `; u+ }; ~, ^; G3 g; Z9 R9 c) | - $sql = "SELECT gid,title FROM ".DB_PREFIX."blog WHERE hide='n' AND type='blog'";
- u3 e/ E! d8 Q; @/ {0 `6 v, @. f0 i' j - if($related_log_type == 'tag'){$log_cache_tags = $CACHE->readCache('logtags');$Tag_Model = new Tag_Model();$related_log_id_str = '0';foreach($log_cache_tags[$logid] as $key => $val){$related_log_id_str .= ','.$Tag_Model->getTagByName($val['tagname']);}! y5 P+ F% h- Z. D
- $sql .= " AND gid!=$logid AND gid IN ($related_log_id_str)";}else{
" S) n. W7 B6 {4 A& g* a - $sql .= " AND gid!=$logid AND sortid=$sortid";}' V b- C$ V _; {
- switch ($related_log_sort){case 'views_desc':{2 ` I, y% J' X; z: l# H
- $sql .= " ORDER BY views DESC";break;}case 'views_asc':{
( Q2 U" m9 }7 K4 m5 Y - $sql .= " ORDER BY views ASC";break;}case 'comnum_desc':{
$ C4 j$ J) [. m J - $sql .= " ORDER BY comnum DESC";break;}case 'comnum_asc':{2 k% W u, _, o" @( u$ I
- $sql .= " ORDER BY comnum ASC";break;}case 'rand':{, x% F9 R; k! Q) e5 G
- $sql .= " ORDER BY rand()";break;}}7 C6 `) B: R2 n" x
- $sql .= " LIMIT 0,$related_log_num";
; ^* R" R4 S% R+ I& ]( ^ - $related_logs = array();$query = $DB->query($sql);while($row = $DB->fetch_array($query))/ m) N1 `; f9 I. ?
- {$row['gid'] = intval($row['gid']);$row['title'] = htmlspecialchars($row['title']);$related_logs[] = $row;}
4 ~4 H+ g9 o* R - $out = '';if(!empty($related_logs)){foreach($related_logs as $val){
: [0 x( r8 ]( ]& P& ^3 C, Y) A4 t! F1 b - $out .= "<li><a href="".Url::log($val['gid'])."" title="{$val['title']}">{$val['title']}</a></li>";* I0 ^: W' a9 T" K
- }}if(!empty($value['content'])){if($related_inrss == 'y'){$abstract .= $out;}}else{echo $out;}}
. A3 w0 F- H$ A+ A, { - ?>
复制代码 第二步:在模板文件echo_log.php的适合位置放入<?php xg_logs($logData);?>,最后写上CSS格式就ok了 |