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

2023-05-10 20:58:52 浏览数 (2)

今天在群里有人想实现侧边栏最新文章获取图片,自己折腾了一阵子,成功解决了这个问题。为了以后再用得着,所以在这儿保存记录。当然除了调出图片,还可以调用当前分类、摘要等,具体看如下代码:首页在module.php中找到最新文章那段代码,直接用以下代码替换即可:

代码语言:javascript复制
<?php //widget:最新文章
function widget_newlog($title){
$index_newlognum = Option::get('index_newlognum');?>
<div class="cbl-one"><div class="title"><p><?php echo $title;?></p></div>
<?php 
$db = MySql::getInstance();
$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])) {
preg_match_all("/<img.*?src="(.*?)"[^>]*>/i",$row['content'],$match);}}else{preg_match_all("/<img.*?src="(.*?)"[^>]*>/i", $row['content'],$match);}
$img = isset($match[0][0]) ? $match[0][0]:'<img src="图片地址" />';//无图片时显示
$date = gmdate('Y年m月d日', $row['date']);
$content = strip_tags($logpost,'');
$content = mb_substr($content,0,100,'utf-8');//摘要字数修改本代码中的100这个即可
$comment = ($row['comnum'] != 0) ? ''.$row['comnum'].'' : '0'; $gid = $row['gid']; $tag = $db -> query("SELECT * FROM ".DB_PREFIX."tag WHERE gid LIKE '%,$gid,%'");?>
<a href="<?php echo Url::sort($row['sid']);?>" title="<?php echo $row['sortname'];?>" /><?php echo $row['sortname'];?></a><br />
<a href="<?php echo Url::log($row['gid']);?>" title="<?php echo $row['title'];?>" /><?php echo $row['title'];?></a><br />
<?php echo $img;?><br />
<?php echo $content;?>
<?php };?></div><?php }?>

随便附带上热门文章的代码 30天内按点击量排序:

代码语言:javascript复制
<?php //widget:热门文章
function widget_hotlog($title){
$index_hotlognum = Option::get('index_hotlognum');?>
<div class="cbl-one"><div class="title"><p><?php echo $title;?></p></div>
<?php $db = MySql::getInstance();$db = MySql::getInstance();
$time = time();
$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");
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])){
preg_match_all("/<img.*?src="(.*?)"[^>]*>/i",$row['content'],$match);}}else{preg_match_all("/<img.*?src="(.*?)"[^>]*>/i", $row['content'],$match);}
$img = isset($match[0][0]) ? $match[0][0]:'<img src="图片地址" />';//无图片时显示
$date = gmdate('Y年m月d日', $row['date']);
$content = strip_tags($logpost,'');
$content = mb_substr($content,0,100,'utf-8');//摘要字数修改本代码中的100这个即可
$comment = ($row['comnum'] != 0) ? ''.$row['comnum'].'' : '0';
$gid = $row['gid'];?>
<li><a href="<?php echo Url::log($row['gid']);?>" title="<?php echo $row['title'];?>" /><?php echo $row['title'];?></a></li>
<?php };?></div><?php }?>

代码说明:(css本人就不折腾了,自己捣鼓吧,如果你这个也不会的话,就不要折腾了)

所属分类链接:<?php echo Url::sort($row['sid']);?>

所属分类标题:<?php echo $row['sortname'];?>

文章链接:<?php echo Url::log($row['gid']);?>

文章标题:<?php echo $row['title'];?>

当前第一张图片:<?php echo $img;?> 

当前文章摘要:<?php echo $content;?>

当前评论:<?php echo $row['comnum'];?>

当前浏览量:<?php echo $row['views'];?>

当前时间:<?php echo gmdate('Y年m月d日', $row['date']);?>

本栏目标题(侧边栏):<?php echo $title;?>

0 人点赞