织梦园模板网专注于织梦企业模板、DeDe整站模板、织梦Html5模板等织梦模板的下载,提供DEDE织梦模板相关服务。织梦园织梦模板交流群
用户名:
密码:
注册
注册后享受折扣价

dede自动动获取上级栏目的标题 关键字 描述

织梦园模板 / 2019-03-16 / 收藏
当我们做大型网站的时候,上百个栏目,为了SEO,在栏目模板里面,我们一般会像下面这样去调用栏目标题、关键字、描述。
<title>{dede:field.seotitle/}</title>
<meta name="keywords" content="{dede:field name='keywords'/}" />
<meta name="description" content="{dede:field name='description' function='html2text(@me)'/}" />
但是这样有一个问题,就是假如个别栏目忘记写标题、关键字、描述就会为空了。这时我们可能就会想,如果能让栏目标题、关键字、描述为空的时候,就调用上级的就好了,当上级没有时候时候,再调用上上级的,如果到顶级栏目都还没有的时候,就调用用网站首页的标题、关键字、描述,想法是可以,但是实现起来可能不容易。今天我就来分享一下我的方法吧。

DEDECMS 自动获取上级栏目的标题,关键字 描述    
***步:打开/include/typelink.class.php  找到这一段代码 $this->TypeInfos = $this->dsql->GetOne($query);   在他下面加上三行代码加好之后如下
     //载入类目信息
     $query = "SELECT tp.*,ch.typename as ctypename,ch.addtable,ch.issystem FROM `idea_arctype` tp left join `idea_channeltype` ch
     on ch.id=tp.channeltype  WHERE tp.id='$typeid' ";
     if($typeid > 0)
     {
         $this->TypeInfos = $this->dsql->GetOne($query);
//填空
$this->TypeInfos['keywords'] = $this->TypeInfos['keywords'] ? $this->TypeInfos['keywords'] : $this->GetTrueInfos('keywords');
$this->TypeInfos['seotitle'] = $this->TypeInfos['seotitle'] ? $this->TypeInfos['seotitle'] : $this->GetTrueInfos('seotitle');
$this->TypeInfos['description'] = $this->TypeInfos['description'] ? $this->TypeInfos['description'] : $this->GetTrueInfos('description');

         if(is_array($this->TypeInfos))
         {
             $this->TypeInfos['tempindex'] = MfTemplet($this->TypeInfos['tempindex']);
             $this->TypeInfos['templist'] = MfTemplet($this->TypeInfos['templist']);
             $this->TypeInfos['temparticle'] = MfTemplet($this->TypeInfos['temparticle']);
         }
     }
第二步:在TypeLink类中增加一个调用方法GetTrueInfos($info) 代码如下:
//为了让某些栏目的关键字描述等不能为空 
   function GetTrueInfos($info = 'seotitle') {		
	$infos = array('reid'=>$this->TypeID,$info=>'');
	while(empty($infos[$info]) && $infos['reid']!=0) {
		$this->dsql->SetQuery("SELECT reid,".$info."  FROM idea_arctype WHERE id='".$infos['reid']."'");
   		$infos = $this->dsql->GetOne();
	}
	if(empty($infos[$info])) {
		if($info == "seotitle") return $GLOBALS['cfg_webname'];
		if($info == "keywords") return $GLOBALS['cfg_keywords'];
		if($info == "description") return $GLOBALS['cfg_description'];
	} 
   	return $infos[$info];
   }

保存之后,这里我们去生成一下预览效果。

相关织梦教程

收缩