今天继续介绍一些简单的博客SEO的设置,为wordpress添加description以及keywords。一般来说,有插件可以实现这个功能,比较有名的就是all in one seo。但是懒人还是想用代码自动插入description以及keywords。
[blink]更多文章参考:
WordPress 添加面包屑导航
[/blink]
wordpress添加description及keywords代码
将以下代码插入到合适位置,如header.php中:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<?php if (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="<?php echo $keywords ?>" /> <meta name="description" content="<?php echo $description ?>" /> <?php } else { ?> <meta name="description" content="<?php //自定义description?>" /> <meta name="keywords" content="<?php //自定义keywords?>" /> <?php } ?> |
大概解释下,就是如果在内容页,那么自动截取前220个字作为description,tag作为keywords。否则将显示自定义的description及keywords。
小结
国外好多主题其实都不加description以及keywords的,但是加上肯定是更好的。