李清海's blog » 为Wordpress添加keyword和description标签
Home > Wordpress > 为WordPress添加keyword和description标签

为WordPress添加keyword和description标签

October 14th, 2009 grubylee 1,351 veiws Leave a comment Go to comments

刚刚查看我博客源代码的时候,发现没有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:

Categories: Wordpress Tags: ,
声明:本站遵循署名-非商业性使用-相同方式共享 3.0共享协议. 转载请注明转自 李清海's blog
  1. October 26th, 2009 at 18:29 | #1

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

  2. January 7th, 2010 at 18:53 | #2

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

  3. January 17th, 2010 at 23:31 | #3

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

  4. May 10th, 2010 at 12:27 | #4

    ok 抄走~~3Q

  5. Huey
    June 8th, 2010 at 10:22 | #5

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

  6. grubylee
    June 8th, 2010 at 14:18 | #6

    @Huey

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

  1. No trackbacks yet.