wordpress本身对SEO优化做的不好,所以要手动做优化。WordPress seo插件All in One SEO Pack 这个插件非常不错。今天教大家用代码来完美替代 All in One SEO 插件为博客做SEO优化。
1、Title 的优化
在主题的 Header.php 中修改 Title 标签为
wp_title( '|', true, 'right' ); bloginfo( 'name' ); $site_description = get_bloginfo( 'description', 'display' ); if ( $site_description && ( is_home() || is_front_page() ) ) echo " | $site_description";
2、关键字、描述、版权、作者 Keywords 与 Description 在SEO中是很重要的元素,把下面这段代码复制到主题的functions.php中
function rbt_head(){ if ( is_home() ){ $description = "设计蜂巢是一个专注于前端开发技术,关注web新型技术的博客。"; $keywords = "设计蜂巢,前端, 前端设计, 前端开发, 设计, 开发, 前端资源, CSS, CSS3.0,div,HTML5,WEB2.0,jquery,JavaScript,Ajax,asp,php,胡俊涛,UI,用户体验,html,xhtml,json,design,w3c,浏览器"; } elseif ( is_single() ){ $post = get_post( $post_ID ); 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 = utf8Substr($post_content,0,220); } $keywords = ""; $tags = wp_get_post_tags($post->ID); foreach ($tags as $tag ) { $keywords = $keywords . $tag->name . ","; } } $out .= ''; $out .= "\n"; $out .= ''; $out .= "\n"; $out .= ''; $out .= "\n"; $out .= ''; $out .= "\n"; $out .= ' '; echo $out; } function utf8Substr($str, $from, $len) { return preg_replace('#^(?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$from.'}'.'((?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$len.'}).*#s','$1',$str); } add_action('wp_head', rbt_head);
3、关于解决存档页、作者页、标签和分类页的”noindex”的问题
有人说在存档页、作者页、标签和分类页加
阻止搜索引擎索引。
个人觉得收录这些页面没什么坏处,只要你的模板设计的合理。如果你想阻止收录这些页面你可以把上面rbt_head()中的
$out .= ''; //改为 if (is_home || is_single() ) $out .=''; else{ $out .= ''; }
学习学习……
这只是首页的设置吧,文章如何代码设置??
这么好的优化代码当然要收藏啦! 🙂
不错的优化代码,值得收藏! 🙂