必应的站长工具一直提示我,有太多页面的描述存在问题,不管是太短或者是重复,总之就是有问题。其中大量页面都来自于心站日志(也就是使用typecho的本站)。
查看源码发现,meta中的description都是自动摘取了文章中的开头部分。因为我的废话连篇以及时不时更新内容,导致描述出现了不准确甚至是冲突的情况。
但翻遍了handsome主题中的设置,也没注意到哪里可以修改或增添该功能。上网查找答案才发现,其实关于这个问题的解决方案在多年前就有网友给出了(见《SEO优化之增加自定义description和keywords标签菜单》一文),只不过我一直没有注意,也从来没太当回事。
以下是依样画葫芦的做法(具体参考见文后),当前主题版本为9.2.1。
首先打开usr/themes/handsome/component/header.php文件,找到第54行,将<?php $this->header(Content::exportGeneratorRules($this)); ?>替换成如下内容:

<?php
    if($this->is('index')){
        $this->header(Content::exportGeneratorRules($this));
    }else{
        $custom_headerb = '';
        if(!is_null($this->fields->description)){
            $custom_headerb .= 'description=';
            $custom_headerb .= $this->fields->description;
        }
        if(!is_null($this->fields->keywords)){
            if(strcmp($custom_headerb,'') == 0){
                $custom_headerb .= 'keywords=';
                $custom_headerb .= $this->fields->keywords;
            }else{
                $custom_headerb .= '&keywords=';
                $custom_headerb .= $this->fields->keywords;
            }
        }
        if(strcmp($custom_headerb,'') == 0){
            $this->header(Content::exportGeneratorRules($this));
        }else{
            $this->header($custom_headerb);
        }
    }
?>

修改header文件.png
然后打开/handsome/functions_mine.php文件,找到function themeFields(Typecho_Widget_Helper_Layout $layout) 函数,在其后(第773行后)添加如下代码:

$description = new Typecho_Widget_Helper_Form_Element_Text('description', NULL, NULL, _t('描述'), _t('简单一句话描述'));$description->input->setAttribute('class', 'text w-100');
    $layout->addItem($description);
    $keywords = new Typecho_Widget_Helper_Form_Element_Text('keywords', NULL, NULL, _t('关键词'), _t('多个关键词用英文下逗号隔开'));$keywords->input->setAttribute('class', 'text w-100');
    $layout->addItem($keywords);

修改functions_mine文件.png

上述两张图片均来自《Typecho handsome 主题网站SEO优化》一文。

参考资料:
SEO优化之增加自定义description和keywords标签菜单
Typecho handsome 主题网站SEO优化

最后修改:2025 年 03 月 05 日
如果觉得我的文章对你有用,请随意赞赏