优化wordpress的Description 和 Keywords
Description 和 Keywords 的重要性已经不像以前那么重要了,但是设置正确的 Description 和 Keywords 还是对你的排名有利。
WordPress 在撰写日志的时候,可以给日志添加摘要(excerpt)和标签(tag),我的做法就是,就如给日志添加了摘要就把摘要做为 Description,如果没有设置摘要的话,则截取文章的前 220 个字作为 Description,而标签直接作为 Keywords。代码如下:
<?if (is_home()){
$description = "RobinS Blog盛彬的博客,在这里你可以看到很多最新的电影,音乐,游戏,另外还有盛彬的近况和他的美好生活,这就是RobinS的网络世界.";
$keywords = "盛彬, RobinS,电影, 博客, 影评";
} 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 相应的位置,同样我也只优化了首页和日志页面。