Emlog非插件实现文章添加相关文章的方法
0
1508
2017年9月30日
Emlog非插件实现文章添加相关文章的方法
第一步:在模板文件module.php中加入以下代码
<?php //文章详情页下相关文章 function xg_logs($logData = array()){ if (is_file($configfile)) {require $configfile;}else{ $related_log_type = 'sort';//相关日志类型,sort为分类,tag为标签; $related_log_sort = 'views_desc';//排列方式,views_desc 为点击数(降序)comnum_desc 为评论数(降序) rand 为随机 views_asc 为点击数(升序)comnum_asc 为评论数(升序) $related_log_num = '10'; //显示文章数 $related_inrss = 'y'; //是否显示在rss订阅中,y为是,其它值为否 }global $value;$DB = MySql::getInstance();$CACHE = Cache::getInstance();extract($logData);if($value) {$logid = $value['id'];$sortid = $value['sortid'];global $abstract;} $sql = "SELECT gid,title FROM ".DB_PREFIX."blog WHERE hide='n' AND type='blog'"; 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']);} $sql .= " AND gid!=$logid AND gid IN ($related_log_id_str)";}else{ $sql .= " AND gid!=$logid AND sortid=$sortid";} switch ($related_log_sort){case 'views_desc':{ $sql .= " ORDER BY views DESC";break;}case 'views_asc':{ $sql .= " ORDER BY views ASC";break;}case 'comnum_desc':{ $sql .= " ORDER BY comnum DESC";break;}case 'comnum_asc':{ $sql .= " ORDER BY comnum ASC";break;}case 'rand':{ $sql .= " ORDER BY rand()";break;}} $sql .= " LIMIT 0,$related_log_num"; $related_logs = array();$query = $DB->query($sql);while($row = $DB->fetch_array($query)) {$row['gid'] = intval($row['gid']);$row['title'] = htmlspecialchars($row['title']);$related_logs[] = $row;} $out = '';if(!empty($related_logs)){foreach($related_logs as $val){ $out .= "<li><a href=\"".Url::log($val['gid'])."\" title=\"{$val['title']}\">{$val['title']}</a></li>"; }}if(!empty($value['content'])){if($related_inrss == 'y'){$abstract .= $out;}}else{echo $out;}} ?>
第二步:在模板文件echo_log.php的适合位置放入<?php xg_logs($logData);?>,最后写上css格式就ok了
这个主题简介啊