为WordPress添加keyword和description标签

刚刚查看我博客源代码的时候,发现没有keyword和description标签,而Google索引的首页描述,经常是我的第一篇文章的内容,并且没有关键字。而文章页面的快照是内容的前100字,也没有关键字。我没有安装SEO插件。还是觉得有必要进行优化下。

下面以我的英文博客Google Support Blog为例,给wordpress添加keyword和description属性。

首先找你主题文件夹里的header.php,部分主题可能是head.php

找到title属性,并在其后面添加如下代码:

<?php if (is_home()){ 
    $description = "Google Support Blog Focus on Google technology!";
    $keywords = "Google Docs,Google Chrome,Chrome theme,Chrome plugin,Google Analystic";
} elseif (is_single()){    
    $description =  substr(strip_tags($post->post_content),0,220);
    $keywords = "";        
    $tags = wp_get_post_tags($post->ID);
    foreach ($tags as $tag ) {
        $keywords = $keywords . $tag->name . ", ";
    }
} 
?>
<meta name="keywords" content="<?=$keywords?>" />
<meta name="description" content="<?=$description?>" />

那么google索引的首页描述应该为“Google Support Blog Focus on Google technology!”,首页关键字应该是“Google Docs,Google Chrome,Chrome theme,Chrome plugin,Google Analystic”。而单页面的话描述内容是文章的前200个字符。关键字则是文章的TAG。大家可以查看我的源文件看看效果。当然你也可以为标签,分类等页面添加keyword、description标签,方法类似啦

至于原理,我就不在这里卖弄了。注意使用的时候把描述和关键字替换成自己的。 :razz:

This entry was posted in Wordpress and tagged , . Bookmark the permalink.

6 Responses to 为WordPress添加keyword和description标签

  1. Kevin Wei says:

    自己写页面的description更好一些哈,可惜,我也懒得自己写…

  2. 呵呵,我正在找呢,google第一个就找到你的了,加上去了,却是修成正果了,多谢拉!

  3. babu says:

    铁通打开都这么快,难道是三线?O(∩_∩)O哈哈~

  4. imceer says:

    ok 抄走~~3Q

  5. Huey says:

    博主有没有注意中文乱码问题~ 本人源码就可看到,网上有解决方法~

  6. grubylee says:

    @Huey

    我博客乱码呢?目前没有发现,如果是你博客乱码的话,修改文件后请保存成 utf-8 格式。 :lol:

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">