wp-wordpress描述插件

添加评论 2010年3月15日

为了更好的优化,一直以来都在寻找能给wordpress添加关键词和描述的插件。试了好多seo方面的插件,因为各种原因,基本上没有令我满意的。今天一个巧合的机会,找到了一段相关的代码。放在wordpress的<head></head>之间。居然表现良好,首页能自定义关键词和描述。其它单独页面能从文章中搜寻关键词及描述。还是先把代码公布一下:

<?php         if (is_single()|| is_page()) {                                 //文章页的关键字和描述
if ($post->post_excerpt) {
$description     = $post->post_excerpt;
} else { $description = substr(strip_tags($post->post_content),0,220); } //取前220个字做描述
$description = str_replace(array(“\r\n”, “\r”, “\n”, “  “), ” “, $description);
$description = str_replace(array(“\”"), “”, $description);  //过滤符号等
$tags = wp_get_post_tags($post->ID);    //取tags做关键字
if(!empty($tags)){
foreach ($tags as $tag ) {
$keywords = $keywords . $tag->name . “,”;
}
}
}elseif(is_category()) {                                  //分类页的关键字和描述,目的是取分类下所有的tag
foreach((get_the_category()) as $category) {
$catname = $category->category_nicename;    //取分类名
$description  = $category->category_description;
}
query_posts(‘category_name=’.$catname);
if (have_posts()) : while (have_posts()) : the_post();
$posttags = get_the_tags();
$count=0;
if ($posttags) {
foreach($posttags as $tag) {
$count++;
if ($count <= 1){
$all_tags_arr[] = $tag -> name; //用 $tag 把 $all_tags_arr 变成多维数组,再由array_unique函数进行处理
}
}
}
endwhile;endif;
if(!empty($all_tags_arr)){
$tags_arr = array_unique($all_tags_arr); //去除重复的tag
$keywords = implode(‘,’,$tags_arr);
}
} else {
$description = “自定义首页的描述”;
$keywords = “自定义首页的关键词”;
}
if (!empty($keywords)){    echo ‘<meta name=”Keywords” content=”‘.$keywords.’” />’; }
if (!empty($description)){    echo ‘<meta name=”Description” content=”‘.$description.’” />’; }
?>

这段代码在wordpress中实现的主要功能如下:

1 .单页、文章页直接把当前的tag标签取出来当关键字,前220字做描述

2. 分类取当前分类的所有tags, 输出5个最新的标签. 描述就是:后台加分类时写的那个描述..

3. 首页或其它页使用自定义的静态关键字和描述.

该段代码需放于<head></head>之间.

试用N天后发现该段代码有异常,详情请看:wp-wordpress描述插件的缺陷

  1. 2010年8月4日 at 06:36 | #1

    I cannot believe this is true!

  2. 2010年8月4日 at 23:36 | #2

    Great idea, thanks for this tip!

  3. 2010年10月17日 at 21:01 | #3

    谢谢,我用的插件,好像比这个省事

  4. 2010年10月17日 at 21:10 | #4

    只是自我感觉噢,不一定对,毕竟插件操作简单

  1. 还没有 trackbacks
订阅评论