建议大家放弃All in one SEO之类的插件,因为它消耗资源,让网站访问速度减慢。现在教你给WordPress首页及文章的Description与Keywords进行SEO优化,通过下面的代码设置博客的 Description 和 Keywords,用日志的摘要作为Description,或是文章的前220个字,用标签(tags)作为关键词 Keywords。用这些代码同样可以达到All in one SEO之类的插件的功能,提高搜索引擎的收录条数。
以下为Title部份代码:
<title><?php global $page, $paged; wp_title( '|', true, 'right' ); bloginfo( 'name' ); $site_description = get_bloginfo( 'description', 'display' ); if ( $site_description && ( is_home() || is_front_page() ) ) echo " | $site_description"; ?></title>
以下为Description与Keywords 优化代码:
<?php
if (!function_exists('utf8Substr')) {
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);
}
}
if (is_home()){
$description = "博客介绍";
$keywords = "关键字,多个关键了以英文“,”号区分开";
} elseif (is_single()){
if ($post->post_excerpt) {
$description = $post->post_excerpt;
} else {
if(preg_match('/<p>(.*)<\/p>/iU',trim(strip_tags($post->post_content,"<p>")),$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 . ",";
}
}
?>
<meta name="description" content="<?php echo trim($description); ?>" />
<meta name="keywords" content="<?php echo rtrim($keywords,','); ?>" />
以上代码已去掉了关键词之间的空格和最后的逗号,也去掉了描述(description)的换行符(\n)。如果第一段日志没有220个字,那可以直接把日志的第一段作为Description。同时解决了substr在截取中文字符时所造成乱码的问题。以上代码唯一的不爽就是没有给分类和页面进行Description与Keywords显示,虽然有解决方法,但过于复杂,所以难得理了。
2010年11月28日更新:经过小明的修改,完全实现WordPress的首页、文章、页面、分类的Description与Keywords显示优化。可以说是完美解决WordPress的Description与Keywords的优化工作。
先把“is_single() ”改成“ have_posts()”,实现页面可以显示Description与Keywords。
再把以下显示分类Description与Keywords代码插在elseif (have_posts())之前:
elseif(is_category()){
$keywords=single_cat_title('', false);
$description= category_description();
$description=str_replace("<p>","",str_replace("</p>","",$description));
}
最终完整代码如下,可以直接拷贝使用:
<?php
if (!function_exists('utf8Substr')) {
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);
}
}
if (is_home()){
$description = "HST网络站--关注 Android、WordPress、互联网、创业、销售、安致、安卓、摄影、时尚、艺术、程序、生活等内容。";
$keywords = "Android,WordPress,安致,安卓,互联网,创业,销售,摄影,时尚,艺术,程序,生活";
} elseif(is_category()){
$keywords=single_cat_title('', false);
$description= category_description();
$description=str_replace("<p>","",str_replace("</p>","",$description));
}elseif (have_posts()){
if ($post->post_excerpt) {
$description = $post->post_excerpt;
} else {
if(preg_match('/<p>(.*)<\/p>/iU',trim(strip_tags($post->post_content,"<p>")),$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 . ",";
}
}
?>
<meta name="description" content="<?php echo trim($description); ?>" />
<meta name="keywords" content="<?php echo rtrim($keywords,','); ?>" />
最终效果如本站,完全实现WordPress的首页、文章、页面、分类的Description与Keywords优化,达到All in one SEO之类的插件的功能,还减少了消耗。在此再次多谢小明。

用上了 非常感谢
不过页面那的话 关键字好像为空
@爱新奇, 因为WordPress的页面没有输入关键字的选项,故没有。
那怎么样把页面的标题作为关键字呢
return preg_replace(‘#^(?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,’.$from.’}’.
‘((?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,’.$len.’}).*#s’,
‘$1’,$str);
这段代码是什么意思,是不是加密过呢?
我怎么设置从[/caption]这个字符串之后开始截取。
是不是用$description=str_replace(“[/caption]“,””,str_replace(“”,””,$description));这样的语句?非常抱歉,我对PHP不怎么懂,请指教,谢谢。
@爱新奇, 这段话的作用是UTF-8截取字时会乱码,用了这段代码后就不会乱码了。
@爱新奇, 其实本代码请拷贝最终完整版进行修改既可。
这代码要复制到哪一个php文件?
从来没有用过那个插件,一直都是用代码解决的~
🙄 修改过的的确不错哇
呵呵,希望对你有用。