当我们做大型网站的时候,上百个栏目,为了SEO,在栏目模板里面,我们一般会像下面这样去调用栏目标题、关键字、描述。
1 | <title>{dede:field.seotitle/}</title> |
2 | <meta name= "keywords" content= "{dede:field name='keywords'/}" /> |
3 | <meta name= "description" content= "{dede:field name='description' function='html2text(@me)'/}" /> |
|
但是这样有一个问题,就是假如个别栏目忘记写标题、关键字、描述就会为空了。这时我们可能就会想,如果能让栏目标题、关键字、描述为空的时候,就调用上级的就好了,当上级没有时候时候,再调用上上级的,如果到顶级栏目都还没有的时候,就调用用网站首页的标题、关键字、描述,想法是可以,但是实现起来可能不容易。今天我就来分享一下我的方法吧。
DEDECMS 自动获取上级栏目的标题,关键字 描述
第一步:打开/include/typelink.class.php 找到这一段代码 $this->TypeInfos = $this->dsql->GetOne($query); 在他下面加上三行代码加好之后如下
02 | $query = "SELECT tp.*,ch.typename as ctypename,ch.addtable,ch.issystem FROM `idea_arctype` tp left join `idea_channeltype` ch |
03 | on ch. id =tp.channeltype WHERE tp. id = '$typeid' "; |
06 | $this->TypeInfos = $this->dsql->GetOne($query); |
08 | $this->TypeInfos[ 'keywords' ] = $this->TypeInfos[ 'keywords' ] ? $this->TypeInfos[ 'keywords' ] : $this->GetTrueInfos( 'keywords' ); |
09 | $this->TypeInfos[ 'seotitle' ] = $this->TypeInfos[ 'seotitle' ] ? $this->TypeInfos[ 'seotitle' ] : $this->GetTrueInfos( 'seotitle' ); |
10 | $this->TypeInfos[ 'description' ] = $this->TypeInfos[ 'description' ] ? $this->TypeInfos[ 'description' ] : $this->GetTrueInfos( 'description' ); |
12 | if (is_array($this->TypeInfos)) |
14 | $this->TypeInfos[ 'tempindex' ] = MfTemplet($this->TypeInfos[ 'tempindex' ]); |
15 | $this->TypeInfos[ 'templist' ] = MfTemplet($this->TypeInfos[ 'templist' ]); |
16 | $this->TypeInfos[ 'temparticle' ] = MfTemplet($this->TypeInfos[ 'temparticle' ]); |
|
第二步:在TypeLink类中增加一个调用方法GetTrueInfos($info) 代码如下:
02 | function GetTrueInfos($info = 'seotitle' ) { |
03 | $infos = array( 'reid' =>$this->TypeID,$info=> '' ); |
04 | while (empty($infos[$info]) && $infos[ 'reid' ]!=0) { |
05 | $this->dsql->SetQuery( "SELECT reid," .$info. " FROM idea_arctype WHERE id='" .$infos['reid ']."' "); |
06 | $infos = $this->dsql->GetOne(); |
08 | if (empty($infos[$info])) { |
09 | if ($info == "seotitle" ) return $GLOBALS[ 'cfg_webname' ]; |
10 | if ($info == "keywords" ) return $GLOBALS[ 'cfg_keywords' ]; |
11 | if ($info == "description" ) return $GLOBALS[ 'cfg_description' ]; |
|
保存之后,这里我们去生成一下预览效果,此方法在DEDECMS 5.7 SP1上测试有效,其它版本未测试。 在此献丑了