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

两天 学习资料937K2字数 8680阅读28分56秒阅读模式

且看之前的两篇文章,关于SEO WP博客的Description、Keywords和Title。(有什么不明白的话也可看这两篇文章,有详细的分析。)
分享SEO WordPress的Description 与 Keywords( http://liucheng.name/930/ )
分享SEO WordPress的标题Title ( http://liucheng.name/932/ )

大家不知有没有看过 我爱水煮鱼 我是如何 SEO WordPress 的 2:Description 和 Keywords。 挺不错的,建议大家放弃All in one SEO之类的插件,改用这段代码就行了。文章源自两天的博客-https://2days.org/2338.html

通过下面的代码设置博客的DescriptionKeywords,用日志的摘要作为Description,或是文章的前220个字,用标签(tags)作为关键词Keywords。
只需要把这段代码放在header.php就行了。文章源自两天的博客-https://2days.org/2338.html

我爱水煮鱼 里的原代码:文章源自两天的博客-https://2days.org/2338.html

  1. <?if (is_home()){      
  2.  $description = "首页的描述";      
  3.  $keywords = "首页的关键词";  
  4. elseif (is_single()){      
  5.  if ($post->post_excerpt) {          
  6.   $description  = $post->post_excerpt;      
  7.  } else {         
  8.   $description = substr(strip_tags($post->post_content),0,220);      
  9.  }        
  10.  $keywords = "";             
  11.  $tags = wp_get_post_tags($post->ID);      
  12.  foreach ($tags as $tag ) {          
  13.   $keywords = $keywords . $tag->name . ", ";      
  14.   }  
  15. }  
  16. ?>  
  17. <meta name="keywords" content="<?=$keywords?>" />  
  18. <meta name="description" content="<?=$description?>" />  

修改版1:
上面的代码已经可以直接拿来用。不过也有一些细节问题,你可以用下面的修改版。主要是去掉了关键词之间的空格和最后的逗号,顺便也去掉了描述(description)的换行符(\n)。文章源自两天的博客-https://2days.org/2338.html

  1. <?php  
  2.  if (is_home()){     
  3.  $description = "首页的描述";      
  4.  $keywords = "首页的关键词";  
  5. elseif (is_single()){     
  6.  if ($post->post_excerpt) {         
  7.   $description  = $post->post_excerpt;      
  8.  } else {         
  9.   $description = substr(strip_tags($post->post_content),0,220);     
  10.  }        
  11.  $keywords = "";             
  12.  $tags = wp_get_post_tags($post->ID);      
  13.  foreach ($tags as $tag ) {         
  14.   $keywords = $keywords . $tag->name . ",";      
  15.   }  
  16. }  
  17. ?>  
  18. <meta name="description" content="<?php echo trim($description); ?>" />  
  19. <meta name="keywords" content="<?php echo rtrim($keywords,','); ?>" />  

修改版2:
下面还有一段代码,主要是应Blinux的要求,呵。他需要把日志的第一段作为Description,就是不要再从第二行取字符了。Blinux是一个完美主义者,所以在他的要求下,也经过测试,下面的代码近乎完美地解决了这个问题。文章源自两天的博客-https://2days.org/2338.html

  1. <?php  
  2.  if (is_home()){  
  3.     $description = "首页的描述";  
  4.     $keywords = "首页的关键词";  
  5. elseif (is_single()){  
  6.     if ($post->post_excerpt) {  
  7.         $description  = $post->post_excerpt;  
  8.     } else {  
  9.    if(preg_match('/<p>(.*)<\/p>/iU',trim(strip_tags($post->post_content,"<p>")),$result)){  
  10.     $post_content = $result['1'];  
  11.    } else {  
  12.     $post_content_r = explode("\n",trim(strip_tags($post->post_content)));  
  13.     $post_content = $post_content_r['0'];  
  14.    }  
  15.          $description = substr($post_content,0,220);     
  16.   }  
  17.    
  18.     $keywords = "";        
  19.     $tags = wp_get_post_tags($post->ID);  
  20.     foreach ($tags as $tag ) {  
  21.         $keywords = $keywords . $tag->name . ",";  
  22.     }  
  23.   
  24. }  
  25.   
  26. ?>  
  27. <meta name="description" content="<?php echo trim($description); ?>" />  
  28. <meta name="keywords" content="<?php echo rtrim($keywords,','); ?>" />  

主要解释一下中间这段:文章源自两天的博客-https://2days.org/2338.html

  1. ###第一种情况,以<p>开始,</p>结束来取第一段  
  2. if(preg_match('/<p>(.*)<\/p>/iU',trim(strip_tags($post->post_content,"<p>")),$result)){   
  3.     $post_content = $result['1'];  
  4. else {  
  5. ###第二种情况,以换行符(\n)来取第一段     
  6.    $post_content_r = explode("\n",trim(strip_tags($post->post_content)));   
  7.     $post_content = $post_content_r['0'];  
  8. }   
  9. ###分两种情况的原因是:第一种情况对Windows Live Writer发布的文章有效。第二种情况适合直接用WP的后台发布的文章。  

代码放在header.php
你可以考虑放在title标签的之前或之后,不过我习惯的作法是把上面的代码单独另存为一个文件,如命名为desc.php,上传到你目前所用主题的目录下,然后调用就行了。文章源自两天的博客-https://2days.org/2338.html

最近又挺多童鞋过来问要关于Page页面和分类页面的Description和Keywords。研究了一下,终于通了。完整版诞生。功能可与All in One SEO 插件相媲美。这下你们是可以彻底丢掉All in One SEO 插件了文章源自两天的博客-https://2days.org/2338.html

推荐的修改方法:

在header.php,原始的title是这样的:文章源自两天的博客-https://2days.org/2338.html

  1. <title><?php wp_title('&laquo;', true, 'right'); ?> <?php bloginfo('name'); ?></title>  

修改为:文章源自两天的博客-https://2days.org/2338.html

  1. <?php include_once("desc.php"); ?>  

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

SEO WordPress的Description、Keywords和Title(通用版)

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

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

  1. <!--Title Begin, By Lc.-->  
  2. <?php if ( is_home() ) { ?><title><?php bloginfo('name'); ?> | <?php bloginfo('description'); ?></title><?php } ?>  
  3. <?php if ( is_search() ) { ?><title>搜索结果 | <?php bloginfo('name'); ?></title><?php } ?>  
  4. <?php if ( is_single() ) { ?><title><?php echo trim(wp_title('',0)); ?> | <?php bloginfo('name'); ?></title><?php } ?>  
  5. <?php if ( is_page() ) { ?><title><?php echo trim(wp_title('',0)); ?> | <?php bloginfo('name'); ?></title><?php } ?>  
  6. <?php if ( is_category() ) { ?><title><?php single_cat_title(); ?> | <?php bloginfo('name'); ?></title><?php } ?>  
  7. <?php if ( is_month() ) { ?><title><?php the_time('F'); ?> | <?php bloginfo('name'); ?></title><?php } ?>  
  8. <?php if (function_exists('is_tag')) { if ( is_tag() ) { ?><title><?php  single_tag_title("", true); ?> | <?php bloginfo('name'); ?></title><?php } ?> <?php } ?>  
  9. <?php  
  10. ##定义一个函数.解决截取中文乱码的问题  
  11. if (!function_exists('utf8Substr')) {  
  12.  function utf8Substr($str$from$len)  
  13.  {  
  14.      return preg_replace('#^(?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$from.'}'.  
  15.           '((?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$len.'}).*#s',  
  16.           '$1',$str);  
  17.  }  
  18. }  
  19. if ( is_home() ){  
  20.     $description = "我们身处急速上涨的数据海洋中…我们如何避免信息的没顶之灾呢?柳城博客(Lc.)∷关注生物信息学,分享学习Linux、PHP+Mysql、Perl/BioPerl等的心得,努力在数据的海洋里畅游。";  
  21.     $keywords = "生物信息学,Perl,Bioperl,PHP,Mysql,Linux,NCBI,摄影";  
  22. }  
  23. elseif ( is_single() ){  
  24.     if ($post->post_excerpt) {  
  25.         $description  = $post->post_excerpt;  
  26.     } else {  
  27.    if(preg_match('/<p>(.*)<\/p>/iU',trim(strip_tags($post->post_content,"<p>")),$result)){  
  28.     $post_content = $result['1'];  
  29.    } else {  
  30.     $post_content_r = explode("\n",trim(strip_tags($post->post_content)));  
  31.     $post_content = $post_content_r['0'];  
  32.    }  
  33.          $description = utf8Substr($post_content,0,220);    
  34.   }  
  35.    
  36.     $keywords = "";       
  37.     $tags = wp_get_post_tags($post->ID);  
  38.     foreach ($tags as $tag ) {  
  39.         $keywords = $keywords . $tag->name . ",";  
  40.     }  
  41. }  
  42. ?>  
  43. <?php echo "\n"; ?>  
  44. <meta name="description" content="<?php echo trim($description); ?>" />  
  45. <meta name="keywords" content="<?php echo rtrim($keywords,','); ?>" />  
  46. <!--Description & Keywords End, By Lc.-->  

 

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

加入Page页面和分类页的的Description和Keywords。强大!!!!首先当然要知道各个分类页面和Page页面的ID了。

  1. <!--###Title Begin, By Lc.###-->  
  2. <?php if ( is_home() ) { ?><title><?php bloginfo('name'); ?> | <?php bloginfo('description'); ?></title><?php } ?>  
  3. <?php if ( is_search() ) { ?><title>搜索结果 | <?php bloginfo('name'); ?></title><?php } ?>  
  4. <?php if ( is_single() ) { ?><title><?php echo trim(wp_title('',0)); ?> | <?php bloginfo('name'); ?></title><?php } ?>  
  5. <?php if ( is_page() ) { ?><title><?php echo trim(wp_title('',0)); ?> | <?php bloginfo('name'); ?></title><?php } ?>  
  6. <?php if ( is_category() ) { ?><title><?php single_cat_title(); ?> | <?php bloginfo('name'); ?></title><?php } ?>  
  7. <?php if ( is_month() ) { ?><title><?php the_time('F'); ?> | <?php bloginfo('name'); ?></title><?php } ?>  
  8. <?php if (function_exists('is_tag')) { if ( is_tag() ) { ?><title><?php  single_tag_title("", true); ?> | <?php bloginfo('name'); ?></title><?php } ?> <?php } ?>  
  9. <?php  
  10. ##定义一个函数.解决截取中文乱码的问题###  
  11. if (!function_exists('utf8Substr')) {  
  12.  function utf8Substr($str$from$len)  
  13.  {  
  14.      return preg_replace('#^(?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$from.'}'.  
  15.           '((?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$len.'}).*#s',  
  16.           '$1',$str);  
  17.  }  
  18. }  
  19. if ( is_home() ){  
  20.     $description = "我们身处急速上涨的数据海洋中…我们如何避免信息的没顶之灾呢?柳城博客(Lc.)∷关注生物信息学,分享学习Linux、PHP+Mysql、Perl/BioPerl等的心得,努力在数据的海洋里畅游。";  
  21.     $keywords = "生物信息学,Perl,Bioperl,PHP,Mysql,Linux,NCBI,摄影";  
  22. }  
  23. elseif ( is_single() ){  
  24.     if ($post->post_excerpt) {  
  25.         $description  = $post->post_excerpt;  
  26.     } else {  
  27.    if(preg_match('/<p>(.*)<\/p>/iU',trim(strip_tags($post->post_content,"<p>")),$result)){  
  28.     $post_content = $result['1'];  
  29.    } else {  
  30.     $post_content_r = explode("\n",trim(strip_tags($post->post_content)));  
  31.     $post_content = $post_content_r['0'];  
  32.    }  
  33.          $description = utf8Substr($post_content,0,220);    
  34.   }  
  35.    
  36.     $keywords = "";       
  37.     $tags = wp_get_post_tags($post->ID);  
  38.     foreach ($tags as $tag ) {  
  39.         $keywords = $keywords . $tag->name . ",";  
  40.     }  
  41. }  
  42. ###这里是分类页面。自行改变is_category的ID。###  
  43. elseif ( is_category(34) ){  
  44.     $description = "生物信息学(Bioinformatics)是一门利用计算机技术研究生物系统之规律的学科。通过实例分析,介绍生物信息学的入学知识,包含生物信息学的数据库等。重点是NCBI的中文教程。";  
  45.     $keywords = "生物信息学,Bioinformatics,NCBI,影响因子";  
  46. }  
  47. ###这里是Page页。同上。多个页面的话自行添加就是###  
  48. elseif ( is_page(2) ){  
  49.     $description = "关于柳城博客(Lc.)的介绍,联系方式,以及网站历程。柳城博客(LIUCHENG.NAME)∷努力在数据的海洋里畅游。";  
  50.     $keywords = "生物信息学,Perl,Bioperl,PHP,Mysql,Linux,NCBI,摄影";  
  51. }  
  52. elseif ( is_page(135) ){  
  53.     $description = "柳城博客(Lc.)的留言板。有什么问题或建议请在这里留言! 我会尽快回复~ 感谢您的支持!!";  
  54.     $keywords = "柳城博客,Lc.,留言板,留言本";  
  55. }  
  56. ?>  
  57. <?php echo "\n"; ?>  
  58. <meta name="description" content="<?php echo trim($description); ?>" />  
  59. <meta name="keywords" content="<?php echo rtrim($keywords,','); ?>" />  
  60. <!--###Description & Keywords End, By Lc.###-->  

~完。

本站文章大部分始于原创,用于个人学习记录,可能对您有所帮助,仅供参考!

weinxin
312379857
←QQ扫一扫添加QQ好友
版权声明:本站原创文章转载请注明文章出处及链接,谢谢合作!
广告也精彩
 最后更新:2013-9-22
  • SEO
  • wordpress
  • Description
  • Keywords
  • Title
  • title优化
评论  9  访客  5  作者  4
    • 鲜活优惠码
      鲜活优惠码 2

      all in one 那个seo插件实在没必要

      • 海景婚纱摄影
        海景婚纱摄影 5

        学的知识太少了,楼主写的代码完全看不懂呢

        • 合肥做网站公司
          合肥做网站公司 1

          深奥啊!-www.dingq.cn

          • 秀爱女性网
            秀爱女性网 0

            代码看的还真是有点烦躁的

            • 朵未
              朵未 5

              我也加了类似的,省的用插件了,挺好~

            匿名

            发表评论

            匿名网友
            :?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:
            确定

            拖动滑块以完成验证