虽然市面上有很不错的 WordPress 标题 SEO 插件:All in One SEO Pack, HeadSpace 2 等, 但是我还是喜欢折腾用代码的方式去控制 WP 的 标题, 标签及描述等的SEO.
1. 我的 WordPress Title SEO 方式是在主题文件 header.php 里面写入下面的代码
<title><?php if (is_home()) {bloginfo('name');} else {echo trim(wp_title('',0));}?></title>
2. WordPress 关键词及描述
<?php
//判断是否为首页
if ( is_home ( ) )
{
$description = "英文SEO, 致力于Google, Yahoo, Bing and Baidu 等搜索引擎优化(SEO) 及折腾 WordPress, Drupal, ECMS!";
$keywords = "英文SEO,Google SEO,Yahoo SEO,Bing SEO,Baidu SEO,搜索引擎优化";
}
//判断是否为文章页
else if ( is_single () )
{
$description = wp_title(",",false,right).mb_substr(strip_tags($post->post_content),0,120,$encoding="UTF-8");
//$description = mb_strimwidth(strip_tags(apply_filters('the_content',$post->post_content)),0,120);
$keywords = "";
$tags = wp_get_post_tags($post->ID);
foreach ($tags as $tag ) {
$keywords = $keywords . $tag->name . ",";
}
}
//判断是否为分类页
else if ( is_category( ) )
{
$description = strip_tags(category_description());
$keywords = single_cat_title("", false);
}
//判断是否为标签页
else if ( is_tag() )
{
$description = single_tag_title('SurDA',false);
$description = "这里是标签[".$description."]下的所有文章";
$keywords = single_tag_title('SurDA',false);
}
//其他都显示文章标题
else {
$description = wp_title("",false);
$keywords = wp_title("",false);
}
?>
<meta name="description" content="<?php echo $description ; ?>" />
<meta name="keywords" content="<?php echo $keywords ; ?>" />
3. 最后一步就是在主题的 Functions.php 中添加描述截取数及输出为utf-8的代码
if (!function_exists('mb_substr')) {
function mb_substr($str, $start, $len = '', $encoding="UTF-8"){
$limit = strlen($str);
for ($s = 0; $start > 0;–$start) {// found the real start
if ($s >= $limit)
break;
if ($str[$s] <= "\x7F")
++$s;
else {
++$s; // skip length
while ($str[$s] >= "\x80" && $str[$s] <= "\xBF")
++$s;
}
}
if ($len == '')
return substr($str, $s);
else
for ($e = $s; $len > 0; –$len) {//found the real end
if ($e >= $limit)
break;
if ($str[$e] <= "\x7F")
++$e;
else {
++$e;//skip length
while ($str[$e] >= "\x80" && $str[$e] <= "\xBF" && $e < $limit)
++$e;
}
}
return substr($str, $s, $e – $s);
}
}
OK, 搞定. 参考了 SurDA 的 Google Html建议 短的元说明 修善办法.
最新评论
您好,国人的 Syntax Highlighter »
Post:2012-01-07 12:59:39你就直接给个链接呗,搜不到啊
Post:2011-12-23 01:35:20最近开始搞drupal了,为的就是以后 »
Post:2011-11-15 02:50:50这位MM竟然会在这里遇到你
Post:2011-11-14 04:54:01瓦,我喜欢这种原创技术笔记博客~ »
Post:2011-11-08 02:00:43好多博文都推荐插件,您的文章给了 »
Post:2011-11-08 01:58:50上面的方法,现在还有效? 不行呀 »
Post:2011-10-08 02:46:15你很厉害 特别是对worldpress的研究啊
Post:2011-10-07 07:05:55