帝国CMS内容页TAG匹配相关文章列表实现教程

    帝国CMS内容页TAG匹配相关文章列表实现教程

    浏览次数: 次
    作者:墨鱼
    信息来源:本站原创
    更新日期: 2014-07-15 23:39:53
    文章简介

    学习如何在帝国CMS内容页中根据TAG匹配并显示相关文章列表,提升用户体验和内容关联性。本教程提供详细代码示例和步骤说明。

    • 正文开始
    • 相关阅读
    • 推荐作品

    功能:

    比如说有3个TAG,每个TAG下有4篇文章,现在指定要显示10篇文章,那么每个TAG要拿出4篇,共有12篇文章,最终会打乱这12篇文章,然后取10篇

    3个TAG,每个TAG下有3篇文章,现在指定要显示10篇文章,那么每个TAG要拿出3篇文章,共有9篇文章

    3个TAG,A有2篇文章,B有5篇文章,C有3篇文章,现在要显示10篇文章,那么A拿出2篇文章,B拿出5篇文章,C拿出3篇文章,共有10篇文章

    如果 A、B、C中除了当前文章,还有相同的文章,那么会扣除重复的那篇文章,只取一次。所以指定的数量会有偏差

    以下代码放到/e/class/userfun.php里!

    <?php
    function user_otherlink($limit=5){
    	global $dbtbpre,$empire,$navinfor,$class_r;
        if($navinfor[infotags]){
        	$classid=(int)$classid;
            $limit=(int)$limit;
            $tbname=$class_r[$navinfor[classid]][tbname];
            $tagarr=explode(",",$navinfor[infotags]);
            $arr=array();
            $num=ceil($limit/count($tagarr));
            foreach($tagarr as $k=>$v){
    			if(count($tagarr)==$k+1){
    				$num=$limit-count($arr);
    			}else{
    				if($i<$num){
    					$num=ceil(($limit-count($arr))/count($tagarr));
    				}		
    			}
                $t=$empire->fetch1("select tagid,num from {$dbtbpre}enewstags where tagname='$v'");
    			if($t[tagid]){
    				$sql=$empire->query("select id from {$dbtbpre}enewstagsdata where tagid=$t[tagid]");
    				$i=0;
    				while($s=$empire->fetch($sql)){
    					if(in_array($s[id],$arr)||$s[id]==$navinfor[id]){
    						continue;
    					}else{
    						$arr[]=$s[id];
    						$i++;
    					}
    					if($i==$num){
    						break;
    					}
    					if(count($arr)==$limit){
    						break 2;
    					}
    				}
    				if($t[num]>$num){
    					$linshi=$t[tagid];
    				}
    			} 
            }
            if(count($arr)>0){
    			$inid=implode(",",$arr);
    			if(count($arr)<$limit&&$linshi){
    				$sql=$empire->query("select id from {$dbtbpre}enewstagsdata where tagid=$linshi and id not in($inid)");
    				while($s=$empire->fetch($sql)){
    					$arr[]=$s[id];
    					if(count($arr)==$limit){
    						break;
    					}
    				}
    			}
    			$inid=implode(",",$arr);
    			$str="";
    			$news=$empire->query("select id,title,titleurl,titlepic from {$dbtbpre}ecms_{$tbname} where id in($inid)");
    			while($n=$empire->fetch($news)){
    				$str.='<li><a href="'.$n[titleurl].'">'.$n[title].'</a></li>';
    			}	
    		}
            return $str;
       	}
    }
    ?>

    内容页用以下代码调用:5为显示数量 自己改

    <?=user_otherlink(5)?>

    关联内容: 帝国CMS TAG 代码
    帝国CMS图片集优化教程:分割输出与提取首张图片代码详解
    « 上一篇 2014年07月14日
    帝国CMS内容页模板SEO优化,设置文章关键词与简介为标题与描述教程
    下一篇 » 2014年07月16日

    如本文对您有帮助,就请墨鱼抽根烟吧!