教你给WordPress的Description与Keywords进行SEO优化

建议大家放弃All in one SEO之类的插件,因为它消耗资源,让网站访问速度减慢。现在教你给WordPress首页及文章的Description与Keywords进行SEO优化,通过下面的代码设置博客的 Description 和 Keywords,用日志的摘要作为Description,或是文章的前220个字,用标签(tags)作为关键词 Keywords。用这些代码同样可以达到All in one SEO之类的插件的功能,提高搜索引擎的收录条数。

以下为Title部份代码:

[sourcecode language="php"]
<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>
[/sourcecode]

以下为Description与Keywords 优化代码:

[sourcecode language="php"]
<?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,’,'); ?>" />
[/sourcecode]

以上代码已去掉了关键词之间的空格和最后的逗号,也去掉了描述(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())之前:
[sourcecode language="php"]
elseif(is_category()){
$keywords=single_cat_title(”, false);
$description= category_description();
$description=str_replace("<p>","",str_replace("</p>","",$description));
}
[/sourcecode]

最终完整代码如下,可以直接拷贝使用:
[sourcecode language="php"]
<?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 = "佐仔志–关注 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,’,'); ?>" />
[/sourcecode]

最终效果如本站,完全实现WordPress的首页、文章、页面、分类的Description与Keywords优化,达到All in one SEO之类的插件的功能,还减少了消耗。在此再次多谢小明


转载请注明转载自:佐仔志本文链接地址: http://www.jinbo123.com/1658.html
订阅本站:http://feed.feedsky.com/jinbo123投稿联系:huangjb.gz[at]gmail.com

文章推荐:

About 佐仔

关注IT、创业、销售、摄影、时尚、政府、艺术、程序、文学、生活等内容。
This entry was posted in 学习笔记 and tagged , , , . Bookmark the permalink.

9 Responses to 教你给WordPress的Description与Keywords进行SEO优化

  1. Pingback: Tweets that mention 教你给WordPress的Description与Keywords进行SEO优化 | 佐仔志 -- Topsy.com

  2. 爱新奇 says:

    用上了 非常感谢
    不过页面那的话 关键字好像为空

  3. 佐仔 says:

    @爱新奇, 因为WordPress的页面没有输入关键字的选项,故没有。

  4. 爱新奇 says:

    那怎么样把页面的标题作为关键字呢

  5. 爱新奇 says:

    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不怎么懂,请指教,谢谢。

  6. 佐仔 says:

    @爱新奇, 这段话的作用是UTF-8截取字时会乱码,用了这段代码后就不会乱码了。

  7. 佐仔 says:

    @爱新奇, 其实本代码请拷贝最终完整版进行修改既可。

  8. zhang says:

    这代码要复制到哪一个php文件?

  9. 远走高飞 says:

    从来没有用过那个插件,一直都是用代码解决的~

发表评论

电子邮件地址不会被公开。 必填项已用 * 标注

*

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>