虽然现在Google 和百度都已经不把 Meta 标签的内容作为排名的因素了,但是添加好的 Meta 对网站的流量还有有帮助的,因为一个偶然的情况,我发现,Google 的搜索结果在某种情况下会直接使用 Meta Description 做该页面的描述,这个发现,证明 Meta Description,还是有用的,至于 Meta Keywords 貌似基本无效了,这个有时用了还会适得其反,会被认为你有过度 SEO 之嫌,当然,这种情况通常发生于百度。
本站的做法
1)对于首页,使用博客的描述作为 Meta Description,自己写适合的关键词作为 Meta Keywords;
2)对于文章页,使用文章的第一段作为 Meta Description,配合我博客的主题,首页的文章摘要也是使用文章的第一段作为 Meta Description 的,然后使用文章的 Tags (标签) 作为 Meta Keywords。
还是那个观点,不喜欢使用插件,所以同样是扔代码到主题的 Functions.php。
实现所需要的代码
<?php
function seo_meta() {
global $post;
$site_description = get_bloginfo( 'description', 'display' );
$post_description = "";
//$site_keywords = "记忆空间, Nikbobo, Nikbobo的博客, 零碎的记忆";
//$post_keywords = "";
//$post_tags = wp_get_post_tags($post->ID);
//foreach ($post_tags as $post_tag) {
//$post_keywords = $post_keywords . $post_tag->name . ", ";
//}
//if ( !empty($post_keywords) )
//$post_keywords = rtrim($post_keywords, ", ");
if(preg_match('/<p>(.*)<\/p>/iU',trim(strip_tags($post->post_content,"<p>")),$matches)) {
$post_description = trim(strip_tags($matches[1]));
} else {
$post_content = explode("\n",trim(strip_tags($post->post_content)));
$post_description = trim(strip_tags($post_content ['0']));
}
//if ( $post_keywords && is_single() ) {
//echo "\n" . '<meta name="keywords" content="' . $post_keywords . '" />' . "\n";
//}
//elseif ( $site_keywords && ( is_home() || is_front_page() ) ) {
//echo "\n" . '<meta name="keywords" content="' . $site_keywords . '" />' . "\n";
//}
if ( $post_description && is_single() ) {
echo "\n" . '<meta name="description" content="' . $post_description . '" />' . "\n";
}
elseif ( $site_description && is_home() ) {
echo "\n" . '<meta name="description" content="' . $site_description . '" />' . "\n";
}
}
add_action('wp_head', 'seo_meta' );
?>
个性化定制
本代码默认不显示 Meta Keywords
当然,如果你觉得 Meta Keywords 还是有那么一点儿用处的话,你可以去掉代码中的注释,即把含有“//”的那一行代码开头的”//”去掉。这样就有了 Meta Keywords 显示了。