为了让Wordpress亲和百度,把合适的keyword Description加人列表页,以前的SEO代码是加在Header.php文件中,只能加到主页和文章页。在谷歌上找了一下,找到解决办法,而且非常好,不用加插件直接加一段代码就好,将下段 代码copy 到主题目录中的 functions.php中的第一个

<?php

后(注意是主题目录):

/* -----------------------------------------------<<描述自动产生器>> */
function utf8_substr($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);}
function head_meta_desc() {
    
$pre_length  = 80; // description length in # words for post/Page
    
global $wp_query, $s;
    
if(is_single() || is_page()) {
        
$post = $wp_query->post;
        
$post_custom = get_post_custom($post->ID);
                                
if(!empty($post->post_excerpt)) { $text = $post->post_excerpt; } else { $text = $post->post_content; }
        
$text = str_replace(array("\r\n", "\r", "\n", "  "), " ", $text);
        
$text = str_replace(array("\""), "", $text);
        
$text = trim(strip_tags($text));
        
$text = explode(' ', $text);
        
if(count($text) > $pre_length) { $l = $pre_length; $ellipsis = '...'; } else { $l = count($text); $ellipsis = ''; }
        
$description = ''for ($i=0; $i<$l; $i++) $description .= $text[$i] . ' '; $description .= $ellipsis;
} elseif (is_home () ) {$description = get_bloginfo('name') . " - " . " 关于人生哲理,人生感悟,励志名言和个人感想,读书看电影感想和个人资料分享等";
} elseif (is_tag() ) {$description = get_bloginfo('name') ."有关 '". single_tag_title('', false) ."'的文章";
} elseif (is_category() ) {$description = get_bloginfo('name') ."有关 '". single_cat_title('', false) ."'的文章";
} elseif (is_search() ) {$description = "有关: '" . wp_specialchars($s) . "'的搜索结果";
} else { $description = "有关: '" . wp_title('', false) ."'的文章"; }
    
if($description) { $temp = $description; $description = utf8_substr($temp, 0, 90) . '..';
        
echo "<meta name=\"description\" content=\"$description\" />\n";
    
}
}
add_action('wp_head', 'head_meta_desc');
// -- END ----------------------------------------
/* -----------------------------------------------
<<关键字自动产生器>>
*/

function tags_category_to_keywords() {
 
if (is_single()){
    
global $post; $keywords = '';
                
foreach( get_the_tags( $post->ID ) as $tags ) { $keywords .= $tags->name . ', '; }
          
foreach( get_the_category( $post->ID ) as $category ) { $keywords .= $category->cat_name . ', '; }
                        
$temp = substr_replace($keywords,"",-2);
      
$keywords = $temp;
} elseif (is_home () ) {$keywords = "人生,人生哲理,人生感悟,人生格言,励志名言,不吃独食,分享人生,分享生活";
} elseif (is_tag() ) {$keywords = single_tag_title('', false);
} elseif (is_category() ) {$keywords = single_cat_title('', false);
} elseif (is_search() ) {$keywords = wp_specialchars($s);
} else { $keywords = substr(wp_title('', false),2,90); }
          
if($keywords) { echo '<meta name="keywords" content="'.$keywords.'" />'. "\n";
                
}
}
add_action('wp_head', 'tags_category_to_keywords');
// -- END ----------------------------------------

注意:首页的关键字和描述要自己输入。记得删除原来的Header.php中的关键字和描述代码,不然会重复。

本文地址:wordpress自动加入keyword Description

与他有一腿的文章:

TAG标签:, , , ,

2 条留言我要留言 »

  •   1 - ShunYea  |  2010-07-10 at 2:54 下午  

    你好,请问你这个用的是什么代码高亮插件?或者不用的话怎么实现的,谢谢。

    [回复]

    zouhu8 回复:

    用的 CoolCode,使用的时候在代码前比如加“<code lang="php">*****代码部分</code>” code可直接在wordpress后台编辑框上面找到。

    [回复]

必填

必填,绝不公开

Spam Protection by WP-SpamFree