EMLOG博文调用方式小姿势大全

2017-9-26 0 PHP代码 蓝优

1.调用随机文章
<?php
//随机文章
function get_randlog($num){
 $Log_Model = new Log_Model();
 $randLogs = $Log_Model->getRandLog($num);?>
<?php foreach($randLogs as $value): ?>
<a href="<?php echo Url::log($value['gid']); ?>" title="<?php echo $value['title']; ?>" ><img src="<?php get_thum($value['gid']); ?>"/></a>
<?php endforeach; ?>
<?php }?>
 2.调用页面内容
<?php
//调用页面内容
function html_page($id,$echo){
 $result = mysql_fetch_array(mysql_query("SELECT * FROM ".DB_PREFIX."blog WHERE gid ='$id'"));
 return $result[$echo];
}?>
使用:
<?php echo html_page("8","content"); ?>
“8”为该页面的id。“content”为字段名。
 3.多功能博文调用(推荐)
<?php 
//多功能博文调用
function eflyBlogList($way,$num){
 //最新
 if($way=="new"){
 $order = "hide='n' and type='blog' order by date DESC";
 //$order = "hide='n' and type='blog' order by top DESC,date DESC";
 }
 //随机
 if($way=="rand"){
 $order = "hide='n' and type='blog' order by rand()";
 }
 //浏览量
 if($way=='views'){
 $order = "hide='n' and type='blog' order by views DESC";
 }
 //置顶
 if($way=='top'){
 $order = "hide='n' and type='blog' and top='y' order by top DESC,date DESC";
 }
 //30天热门
 if($way=='hot'){
 $time = time();
 $order = "hide='n' and type='blog' and date > $time - 30*24*60*60 order by views DESC";
 }
 //评论量
 if($way=='comnum'){
 $order = "hide='n' and type='blog' order by comnum DESC";
 }
 $db = MySql::getInstance();
 $sql = "select * from ".DB_PREFIX."blog where $order limit 0,$num";
 $log = $db->query($sql);
 while($row = $db->fetch_array($log)){
 $row['title'] = htmlspecialchars($row['title']);
 ?>
<li>
  <img src="<?php get_thum($row['gid']);?>"/>
  <h4><a href="<?php echo Url::log($row['gid']); ?>"><?php echo $row['title']; ?></a></h4>
  日期:<?php echo gmdate('Y-m-d', $row['date']); ?>
  分类:<?php blog_sort($row['gid']); ?>
  浏览:<?php echo $row['views']; ?>
  评论:<?php echo $row['comnum']; ?>
</li>
<?php }?>
<?php }?>
使用方法:
<?php eflyBlogList("new",6);?>
 4.相关文章调用
<?php
//文章详情页下相关文章
function related_logs($logid,$sortid){
 $related_log_type = 'sort';//调用类型,sort为分类,tag为标签;
 $related_log_sort = 'views_desc';//排列方式
 $related_log_num = '10'; //显示文章数
 $DB = MySql::getInstance();
 $CACHE = Cache::getInstance();
 $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'])."\">{$val['title']}</a></li>";
 }
 }
 echo $out;
}?>
在echo_log.php适当位置使用:
<?php related_logs($logid,$sortid);?>

蓝优网说明:可按浏览量、评论量、随机等三种形式调用与当前文章同分类或者同标签的其他文章。

 5.调取指定分类的最新文章

<?php
//调取指定分类的最新文章
function get_sortlist($sortid,$num){
 $db = MySql::getInstance();
 $sql = "SELECT * FROM ".DB_PREFIX."blog WHERE type='blog' AND sortid IN (".$sortid.") AND hide='n' ORDER BY `date` DESC LIMIT 0,".$num."";
 $list = $db->query($sql);
 while($row = $db->fetch_array($list)){
 ?>
    <li><span><?php echo gmdate('Y-m-d',$row['date']);?></span><a href="<?php echo Url::log($row['gid']);?>"><?php echo $row['title'];?></a></li> 
 <?php }?>
<?php } ?>
单个分类:
<?php get_list(1,10);?>或者<?php get_list("1",10);?>
多个分类:
<?php get_list("1,3",10);?>
 6.调取指定分类的最新文章和分类名称及链接
<?php
//调取指定分类的最新文章和分类名称及链接
function get_CMSlist($sortid,$num){
 $db = MySql::getInstance();
 $sql1 = "SELECT sortname FROM ".DB_PREFIX."sort WHERE sid=".$sortid;
 $s = $db->query($sql1);
 $sortname = $db->fetch_array($s);?>
    <h3>分类:<a href="<?php echo Url::sort($sort);?>"><?php echo $sortname['sortname'];?></a></h3>
 <?php 
    $sql2 = "SELECT gid,title,date FROM ".DB_PREFIX."blog WHERE sortid=".$sortid."  AND hide='n' ORDER BY `date` DESC LIMIT $num";
    $list = $db->query($sql2);
    while($row = $db->fetch_array($list)){
    ?>
    <li><span><?php echo gmdate('Y-m-d',$row['date']);?></span><a href="<?php echo Url::log($row['gid']);?>"><?php echo $row['title'];?></a></li>
<?php }?>
<?php }?>
每次只能调用一个分类,调用方式:
<?php get_CMSlist(1,10);?>
 7.配合“模板设置”插件,调取指定多个分类的最新文章和分类名称及链接
可在后台设置要显示的分类和文章数量。
<?php
function get_CMSlist_more(){
 $db = MySql::getInstance();
 global $CACHE; 
 $sort_cache = $CACHE->readCache('sort');
 foreach(_g('CMSsortid') as $key => $i){
 $key = $key+1;
 $out .='<h3><a href="'.Url::sort($i).'">'.$sort_cache[$i]['sortname'].'</a></h3>';
 $out .='<ul>';
 $num = _g('CMSsortlogNum');
 $num = $num - 0;
 $logs = $db->query ("SELECT * FROM ".DB_PREFIX."blog WHERE sortid='$i' AND type='blog' AND hide='n' order by date DESC limit 0,{$num}");
 while ($trow = $db->fetch_array($logs)){
 $date = gmdate('Y-m-d', $trow['date']);
 //$trow['title'] = mb_substr($trow['title'],0,180,'utf-8');
 //$img = get_thum($trow['gid']);
 $out .='<li><span>'.$date.'</span><a href="'.Url::log($trow['gid']).'">'.$trow['title'].'</a></li>';
 }
 $out .='</ul>';
 }
 echo $out;
}?>
options.php中:
<?php
/*@support tpl_options*/
!defined('EMLOG_ROOT') && exit('access deined!');
$options = array(
 
'CMSsortid' => array(
'type' =>'sort',
'name' =>'CMS分类id',
'description' =>'"不选"为不显示,排序请在分类中进行',
'multi' =>'true',
),
 
'CMSsortlogNum' => array(
'type' =>'text',
'name' =>'CMS分类文章数量',
'default' =>'10',
),
 
);
?>
使用:
<?php get_CMSlist_more();?>


标签: EMLOG教程 转载
版权声明:若无特殊注明,本文皆《蓝优》原创,转载请保留文章出处。
本文链接:EMLOG博文调用方式小姿势大全 - http://www.92mo.cn/daima/21.html

发表新评论

快捷功能: