我爱水煮鱼 » WordPress » 我是如何 SEO WordPress 的 2:Description 和 Keywords

我是如何 SEO WordPress 的 2:Description 和 Keywords

Description 和 Keywords 的重要性已经不像以前那么重要了,但是设置正确的 Description 和 Keywords 还是对你的排名有利。

WordPress 在撰写日志的时候,可以给日志添加摘要(excerpt)和标签(tag),我的做法就是,就如给日志添加了摘要就把摘要做为 Description,如果没有设置摘要的话,则截取文章的前 220 个字作为 Description,而标签直接作为 Keywords。代码如下:

<?if (is_home()){
    $description = "我爱水煮鱼是一个关注 WordPress 开源博客平台应用和互联网的 IT 博客。";
    $keywords = "WordPress, 博客, 互联网, 主题, 插件";
} elseif (is_single()){
    if ($post->post_excerpt) {
        $description     = $post->post_excerpt;
    } else {
        $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?>" />

上面代码请放到 header.php 相应的位置,同样我也只优化了首页和日志页面。

标签:

分享到:

请选择你看完该文章的感受:

不错 超赞 无聊 扯谈 不解 路过
  1. 关键词的逗号后面会有个空格,刚开始我以为是 $keywords = $keywords . $tag->name . “, “; 逗号后面有个空格的关系,就改了一下,结果导致全部出现乱码,请问怎样能解决这问题呢?

  2. 和#46楼的同样的问题,

    要解决前220字符换行的问题,#45楼笑林广记的代码应该放到哪个地方?

    $description = str_replace(array(”\r\n”, “\r”, “\n”, ” “), ” “, $description);
    $description = str_replace(array(”\””), “”, $description);

  3. 看到楼上的问题没人回答,我就把我自己的沾上来吧
    $dn=$description = utf8_trim(substr(strip_tags($post->post_content),0,220));
    $dn = str_replace(array(“\r\n”,”\r”,”\n”,”\”",” “),”",$dn);
    详见:我的博客simplc.cn中:让你丢掉all in one seo pack方法

  4. 很好很强大,特来感谢,在你博客中看到很多我想去用代码实现可是还没有思路的东西,太喜欢了。

  5. 已经使用这个方法,主页和文章页都很好,
    但请问:如何把page页面的
    <meta name="keywords" content="” />
    <meta name="description" content="” />
    去掉呢?
    谢谢了

    • <meta name=”keywords” content=”” />
      <meta name=”description” content=”” />

      这个放到下面的 判断里面

      if(is_home()) || is_single()){

      }

  6. $post->post_excerpt;
    显然这段代码是放在the loop之前的,我从网上查到的资料来看,$post标签是在the_post()被调用之后才赋值的,显然,这里把代码放在header.php中,也就肯定是在the loop之外,所以,如果按照“官方”的说法,这样调用$post是没有值的,或者是上一次the_post的错误的值,不是当前页面的值。
    而实践得知,$post确实是当前页面的值。这样就和网上的说法矛盾了。
    求解释!

  7. 感谢分享,参考了网上很多资料,最终还是会选择你的这种方式,他比较高效率,简单实用,,不过avchive页面我可能要适当加个优化代码。。。