• 河南网站建设-郑州网站设计-郑州网站建设-手机建站-郑州建站-上海建站

  • 专注网站建设 服务热线: 13061801310

当前位置:好东东网 > 近期动态 > 网站建设观点 > 正文

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

发布时间:2021-04-30 | 发布者: 东东工作室 | 浏览次数:
当我们做大型网站的时候,上百个栏目,为了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];
   }
 
保存之后,这里我们去生成一下预览效果,此方法在DEDECMS 5.7 SP1上测试有效,其它版本未测试。 在此献丑了
 

转载请标注:东东工作室——dede自动动获取上级栏目的标题 关键字 描述