meta的优化
discuz的后台可以对meta信息进行设置,甚至可以添加自己的头部信息,但它的设置都是针对于所有页面的,全部页面都拥有相同的keywords和description是SEO所不赞成的。
方案一:删除meta
修改页头模板文件 templates/default/header.htm:将meta的keywords和description标签删除。
这两个标签作用很小了,而且还有discuz自带的一些无用信息,用得不好反而会有坏作用,因此宁缺勿滥。
方案二:定制meta
本部分实现了将内容页keywords设为帖子标题,description为内容前100字;也实现了主页与各版列表页meta的单独设置(不同版的不同,同一版各列表页相同)。
1.修改页头模板文件 templates\default\header.htm:将meta的keywords和description标签改为如下形式
<meta name="keywords" content="{$metakeywords}$seokeywords" />
<meta name="description" content="$seodescription" />
这里的$seokeywords、$seodescription就是后台设置的那个值,下面说怎么定制这个值;{$metakeywords}是奇虎的关键字,留下以后处理 字串2
2.内容页(viewthread)设置keywords为帖子标题,description为内容前100字
2.1修改 viewthread.php 文件:
在 include template(''viewthread''); (更新:dz5.5为iinclude template($iscircle ? ''supesite_viewthread'' : ''viewthread''); ) 语句的上面加入一行:
require_once DISCUZ_ROOT.''./include/bmt.thread.inc.php'';
2.2创建 include/bmt.thread.inc.php 文件,内容为
<?php
if(!defined(''IN_DISCUZ'')) { exit(''Access Denied''); }
$seokeywords = strip_tags($thread[''subject'']); //关键字设为帖子的标题
//(已更新: nethome 提出问题,当启用主题分类并允许按类别浏览时,原来代码会有问题。所以加了标签过滤,本来在viewthread.php中改更好,为以后升级方便,还是放在这里吧,subject很短,不会影响效率)
$seodescription = current( $postlist );//description取文章内容的前100字
$seodescription = mb_substr( $seodescription[''message''],0,100,"gb2312" );
字串8
$seodescription = htmlspecialchars( strip_tags($seodescription) );
?>
*此处数字和个别函数适用于GBK版本
最后一行作用是过滤内容中的html,否则在meta中会引起语法错误。先是去除HTML标签,但因为这里是前100字,有可能html标签已经被截断了,所以又用了htmlspecialchars转义一下,有可能会有些垃圾信息。
当然也可以在截取之前用strip_tags去除html标签,可能效率会差了。
*所以此处表达式您需要根据自己的情况修改。
我目前用的是preg_replace( ''/[^\xa1-\xff]/'', '''', $seodescription ),即过滤汉字以外的所在内容,但这样会损失英文关键字。
别外说明,此处数据都是viewthread.php已取好的,所以不会产生额外的数据库操作,只是做了字符串处理,不会引响效率。
3.列表页(forumdisplay)设置不同的keywords和description
3.1修改 forumdisplay.php
字串7
文件,在 include template(''forumdisplay''); 语句上面添加
require_once DISCUZ_ROOT.''./include/bmt.forum.inc.php'';
3.2创建 include/bmt.forum.inc.php 文件,内容为
<?php
if(!defined(''IN_DISCUZ'')) { exit(''Access Denied''); }
$seokeywords = $forum[''name''];
$seodescription = $forum[''description''];
switch ( $forum[''fid''] ){
case 1: //此数字为版的ID号,不同版设置不同的meta
$seokeywords = ''key1,key2,...'';
$seodescription = ''xxxx x








