SEO WordPress的Description、Keywords和Title(完整版)

且看之前的两篇文章,关于SEO WP博客的Description、Keywords和Title。(有什么不明白的话也可看这两篇文章,有详细的分析。)

分享SEO WordPress的Description 与 Keywords( http://liucheng.name/930/
分享SEO WordPress的标题Title ( http://liucheng.name/932/ )
最近又挺多童鞋过来问要关于Page页面和分类页面的Description和Keywords。研究了一下,终于通了。完整版诞生。功能可与All in One SEO 插件相媲美。这下你们是可以彻底丢掉All in One SEO 插件了。

推荐的修改方法:
在header.php,原始的title是这样的:

<title><?php wp_title('&laquo;', true, 'right'); ?> <?php bloginfo('name'); ?></title>修改为:

<?php include_once("desc.php"); ?>你只要把下面的代码保存在desc.php就OK了。(如果有中文的话,记得另存为文件的编码是UTF-8。切记)。把desc.php上传到你的主题的目录下。

SEO WordPress的Description、Keywords和Title(通用版)
Title是覆盖所有的页面的了。通用版的Description和Keywords就只有首页和文章页面的。

把下面的代码另存为desc.php就行了。细节问题自行改动。

<!--Title Begin, By Lc.-->
<?php if ( is_home() ) { ?><title><?php bloginfo('name'); ?> | <?php bloginfo('description'); ?></title><?php } ?>
<?php if ( is_search() ) { ?><title>搜索结果 | <?php bloginfo('name'); ?></title><?php } ?>
<?php if ( is_single() ) { ?><title><?php echo trim(wp_title('',0)); ?> | <?php bloginfo('name'); ?></title><?php } ?>
<?php if ( is_page() ) { ?><title><?php echo trim(wp_title('',0)); ?> | <?php bloginfo('name'); ?></title><?php } ?>
<?php if ( is_category() ) { ?><title><?php single_cat_title(); ?> | <?php bloginfo('name'); ?></title><?php } ?>
<?php if ( is_month() ) { ?><title><?php the_time('F'); ?> | <?php bloginfo('name'); ?></title><?php } ?>
<?php if (function_exists('is_tag')) { if ( is_tag() ) { ?><title><?php  single_tag_title("", true); ?> | <?php bloginfo('name'); ?></title><?php } ?> <?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 = "我们身处急速上涨的数据海洋中…我们如何避免信息的没顶之灾呢?柳城博客(Lc.)∷关注生物信息学,分享学习Linux、PHP+Mysql、Perl/BioPerl等的心得,努力在数据的海洋里畅游。";
    $keywords = "生物信息学,Perl,Bioperl,PHP,Mysql,Linux,NCBI,摄影";
}
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 . ",";
    }
}
?>
<?php echo "\n"; ?>
<meta name="description" content="<?php echo trim($description); ?>" />
<meta name="keywords" content="<?php echo rtrim($keywords,','); ?>" />
<!--Description & Keywords End, By Lc.-->SEO WordPress的Description、Keywords和Title(完整版)
加入Page页面和分类页的的Description和Keywords。强大!!!!首先当然要知道各个分类页面和Page页面的ID了。

 <!--###Title Begin, By Lc.###-->
<?php if ( is_home() ) { ?><title><?php bloginfo('name'); ?> | <?php bloginfo('description'); ?></title><?php } ?>
<?php if ( is_search() ) { ?><title>搜索结果 | <?php bloginfo('name'); ?></title><?php } ?>
<?php if ( is_single() ) { ?><title><?php echo trim(wp_title('',0)); ?> | <?php bloginfo('name'); ?></title><?php } ?>
<?php if ( is_page() ) { ?><title><?php echo trim(wp_title('',0)); ?> | <?php bloginfo('name'); ?></title><?php } ?>
<?php if ( is_category() ) { ?><title><?php single_cat_title(); ?> | <?php bloginfo('name'); ?></title><?php } ?>
<?php if ( is_month() ) { ?><title><?php the_time('F'); ?> | <?php bloginfo('name'); ?></title><?php } ?>
<?php if (function_exists('is_tag')) { if ( is_tag() ) { ?><title><?php  single_tag_title("", true); ?> | <?php bloginfo('name'); ?></title><?php } ?> <?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 = "我们身处急速上涨的数据海洋中…我们如何避免信息的没顶之灾呢?柳城博客(Lc.)∷关注生物信息学,分享学习Linux、PHP+Mysql、Perl/BioPerl等的心得,努力在数据的海洋里畅游。";
    $keywords = "生物信息学,Perl,Bioperl,PHP,Mysql,Linux,NCBI,摄影";
}
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 . ",";
    }
}
###这里是分类页面。自行改变is_category的ID。###
elseif ( is_category(34) ){
    $description = "生物信息学(Bioinformatics)是一门利用计算机技术研究生物系统之规律的学科。通过实例分析,介绍生物信息学的入学知识,包含生物信息学的数据库等。重点是NCBI的中文教程。";
    $keywords = "生物信息学,Bioinformatics,NCBI,影响因子";
}
###这里是Page页。同上。多个页面的话自行添加就是###
elseif ( is_page(2) ){
    $description = "关于柳城博客(Lc.)的介绍,联系方式,以及网站历程。柳城博客(LIUCHENG.NAME)∷努力在数据的海洋里畅游。";
    $keywords = "生物信息学,Perl,Bioperl,PHP,Mysql,Linux,NCBI,摄影";
}
elseif ( is_page(135) ){
    $description = "柳城博客(Lc.)的留言板。有什么问题或建议请在这里留言! 我会尽快回复~ 感谢您的支持!!";
    $keywords = "柳城博客,Lc.,留言板,留言本";
}
?>
<?php echo "\n"; ?>
<meta name="description" content="<?php echo trim($description); ?>" />
<meta name="keywords" content="<?php echo rtrim($keywords,','); ?>" />
<!--###Description & Keywords End, By Lc.###-->~完。


本文详细出处参考:http://liucheng.name/1127/

posted on 2010-03-23 17:24  haven24  阅读(5117)  评论(0编辑  收藏  举报

导航