# WordPress 的 Description和Keywords优化设置 > Description 和 Keywords 的重要性已经不像以前那么重要了,但是设置正确的 Description 和 Keywords 还是对你的排名有利。 这两天翻了许多人的博客,测试基本都没有问题但是用着用着就发现了许多的bug.这里将改好的确定无错的代码提上去 免得大家找的辛苦 WordP Description 和 Keywords 的重要性已经不像以前那么重要了,但是设置正确的 Description 和 Keywords 还是对你的排名有利。 这两天翻了许多人的博客,测试基本都没有问题但是用着用着就发现了许多的bug.这里将改好的确定无错的代码提上去 免得大家找的辛苦 WordPress 在撰写日志的时候,可以给日志添加摘要(excerpt)和标签(tag),我的做法就是,就如给日志添加了摘要就把摘要做为 Description,如果没有设置摘要的话,则截取文章的前 220 个字作为 Description,而标签直接作为 Keywords。代码如下: > if (is\_home()){ > $description = "广告诱惑我们,买车子,衣服.于是拼命工作买不需要的东西."; > $keywords = "广告诱惑我们,买车子,衣服.于是拼命工作买不需要的东西."; > } elseif (is\_single()){ > if ($post->post\_excerpt) { > $description = $post->post\_excerpt; > } else { > if(preg\_match('/
(.\*)<\\/p>/iU',trim(strip\_tags($post->post\_content,"
")),$result)){ > $post\_content = $result\['1'\]; > } else { > $post\_content\_r = explode("\\n",trim(strip\_tags($post->post\_content))); > $post\_content = $post\_content\_r\['0'\]; > } > $description = substr($post\_content,0,220); > } > > $keywords = ""; > $tags = wp\_get\_post\_tags($post->ID); > foreach ($tags as $tag ) { > $keywords = $keywords . $tag->name . ","; > } > } > ?> > > 上面代码请放到 header.php 相应的位置,同样我也只优化了首页和日志页面。 --- **分类**:建站 **标签**:wordpress · description · keywords **作者**:子龙 **链接**:https://octohz.com/p/2378