【SEO优化】网站关键字,标题,描述优化

2015年2月16日原创教程 建站历程35,1211
摘要

搞了几个晚上的成就,网站的seo优化代码,不过还有几个地方改改就好了。

生命不息,折腾不止。文章源自狐狸影视城-https://fox-studio.net/18782.html

SEO是网站优化,对于搜索引擎更加友好的过程。如果你不明白可以点击这里文章源自狐狸影视城-https://fox-studio.net/18782.html

以前用的知更鸟的主题确实不错,速度快还优化好,不过现在返回不去了,很多文章是不能导入的。文章源自狐狸影视城-https://fox-studio.net/18782.html

下面是以前网站的样子。文章源自狐狸影视城-https://fox-studio.net/18782.html

【SEO优化】网站关键字,标题,描述优化文章源自狐狸影视城-https://fox-studio.net/18782.html

而且现在主题定制性较高,方便做效果。对于想我这样爱折腾的人是爱不释手的。但是弊端也很大,自从用上了这个主题,各种问题不断。从没有让我好过过,前几天刚刚怒发冲冠,把服务器系统重装,更换环境,然后删除无用的注册表,逐一导入需要的信息。文章源自狐狸影视城-https://fox-studio.net/18782.html

这两天又接着搞网站优化。唉,我是影视从业者,只是玩玩网站。文章源自狐狸影视城-https://fox-studio.net/18782.html

打开一个网页,右键→查看源代码。这些代码才是给搜索引擎看的,它们和咱们看到的东西不一样。可知道做网站一天就搞这些代码,酷毙死了。文章源自狐狸影视城-https://fox-studio.net/18782.html

其中下图中的这些标签是搜索引擎最喜欢的,也是搜索结果中显示的内容。文章源自狐狸影视城-https://fox-studio.net/18782.html

【SEO优化】网站关键字,标题,描述优化文章源自狐狸影视城-https://fox-studio.net/18782.html

之前的主题只有标题<title>标签,描述和标签都没有。之后我加进来的。文章源自狐狸影视城-https://fox-studio.net/18782.html

我把代码放出来,其中我已经加上标注了,因为我是小白,就不详细介绍了。文章源自狐狸影视城-https://fox-studio.net/18782.html

我在修改的时候,除了百度,最有用的是W3C的网,也是http的什么创始网站啥的,反正是个网络界的第一人。不过就是用他站内搜索休要访问google服务器。文章源自狐狸影视城-https://fox-studio.net/18782.html

需要解释的我已经添加在代码中了文章源自狐狸影视城-https://fox-studio.net/18782.html

但是wordpress的页面,标签,归档等分类是没有描述和标签的。文章源自狐狸影视城-https://fox-studio.net/18782.html

你可以将以下代码加入主题founctions.php文件中,从而使用描述和标签功能。文章源自狐狸影视城-https://fox-studio.net/18782.html

再配合上面的代码,就可以在每个页面显示标题,标签和描述。文章源自狐狸影视城-https://fox-studio.net/18782.html

查看SEO代码文章源自狐狸影视城-https://fox-studio.net/18782.html

  1. <?php
  2. /* SEO优化代码,源码自“HotNewspro”主题改写。
  3. ** 根据提示自行修改,请保留作者信息。 
  4. */
  5. ?>
  6. <!-- 狐狸影视城SEO优化代码 -->
  7. <?php echo "n"; ?>
  8. <?php     //开始 Title(标题)截取。 ?>
  9. <?php     //主页
  10. if ( is_home() || is_front_page()) { ?>
  11. <title><?php bloginfo('name'); ?> | <?php bloginfo('description'); ?></title>
  12. <?php } ?>
  13. <?php     //搜索页面
  14. if ( is_search() ) { ?>
  15. <title>搜索结果 | <?php bloginfo('name'); ?></title>
  16. <?php } ?>
  17. <?php     //文章页面
  18. if ( is_single() ) { ?>
  19. <title><?php echo trim(wp_title('',0)); ?><?php if (get_query_var('page')) { echo '-第'; echo get_query_var('page'); echo '页';}?> | <?php bloginfo('name'); ?></title>
  20. <?php } ?>
  21. <?php     //page页面
  22. if ( is_page() && !is_front_page() ) { ?>
  23. <title><?php echo trim(wp_title('',0)); ?> | <?php bloginfo('name'); ?></title>
  24. <?php } ?>
  25. <?php     //分类页面 
  26. if ( is_category() ) { ?>
  27. <title><?php single_cat_title(); ?> | <?php bloginfo('name'); ?></title>
  28. <?php } ?>
  29. <?php     //日期归档 
  30. if ( is_month() ) { ?>
  31. <title><?php the_time('F'); ?> | <?php bloginfo('name'); ?></title>
  32. <?php } ?>
  33. <?php     //标签 
  34. if (function_exists('is_tag')) {
  35. if ( is_tag() ) { ?>
  36. <title><?php single_tag_title(""true); ?> | <?php bloginfo('name'); ?></title>
  37. <?php } ?> <?php } ?>
  38. <?php     //作者 
  39. if ( is_author() ) {?>
  40. <title><?php wp_title('');?>发表的所有文章 | <?php bloginfo('name'); ?></title>
  41. <?php }?>
  42. <?php     //结束 Title(标题)和截取。 ?>
  43. <?php
  44. if (!function_exists('utf8Substr')) {
  45.    function utf8Substr($str, $from, $len)
  46.    {
  47.    return preg_replace('#^(?:[x00-x7F]|[xC0-xFF][x80-xBF]+){0,'.$from.'}'. '((?:[x00-x7F]|[xC0-xFF][x80-xBF]+){0,'.$len.'}).*#s', '$1',$str);     //正则表达式的搜索和替换 
  48.    }     //编码声明
  49. }
  50. if ( is_single() || is_page() ){
  51.    if ($post->post_excerpt) {     //如果摘录有
  52.       $description  = str_replace("n"," ",$post->post_excerpt);     //拾取文章摘录
  53.    } else {
  54.    //如果没有摘录,则从正文内容中截取部分作为摘录。
  55.        if(preg_match('/<p>(.*)</p>/iU',trim(strip_tags($post->post_content)),$result))       {
  56.           $post_content = $result['1'];
  57.        } else {
  58.           $post_content = str_replace("n"," ",trim(strip_tags($post->post_content)));
  59.        }
  60.        //如果您的网站使用了如 Visual Composer 插件。他会生成许多短代码,下面是匹配这个插件而做的删除短代码。
  61.        if(strstr($post_content,'
  62. ') || strstr($post_content,'[mk') ){
  63.           $post_content = str_replace('&nbsp;', '', $post_content);
  64.           $post_content_r = explode("][",$post_content);       //从“][”符号分数组
  65.           $description = "";
  66.           foreach ($post_content_r as $post_content ) {
  67.           $srt1 = strcspn($post_content,"]")+"1";   //查找次一个“]”符号的位置
  68.           $srt2 = Substr($post_content,$srt1);      //从$srt1位置向后拾取字符串
  69.           $srt3 = strcspn($srt2,"[");               //从$srt2字符串查找第一个“[”位置
  70.           $srt4 = Substr($srt2,$srt3);              //从$srt3位置向后拾取$srt2字符串
  71.           $srt5 = array($srt4=>"");                 //定义将拾取的$srt4字符串替换为空
  72.           $srt6 = strtr($srt2,$srt5);               //替换$srt2字符串中定义的内容$srt4
  73.           $description = $description . $srt6 ;
  74.        }
  75.       if(strlen($description)>200){$shengluehao = "...";}
  76.         $description = utf8Substr($description,0,200).$shengluehao;
  77.       }else{
  78.       //如果没有使用短代码,则默认输出。
  79.         if(strlen($description)>200){$shengluehao = "...";}
  80.         $description = utf8Substr($post_content,0,200).$shengluehao;
  81.    }
  82. }
  83.     $keywords = "";
  84.     $tags = wp_get_post_tags($post->ID);
  85.     $tags = wp_get_post_tags($post->ID);
  86.     foreach ($tags as $tag ) {
  87.         $keywords = $keywords . $tag->name . ",";
  88.     }
  89. }
  90. ?>
  91. <?php if ( is_single() ) { ?>
  92. <meta name="description" content="<?php echo trim($description); ?>" /><?php echo "n"; ?>
  93. <meta name="keywords" content="<?php echo rtrim($keywords,','); ?>" /> <?php } ?>
  94. <?php if ( is_page() ) { ?>
  95. <meta name="description" content="<?php echo trim($description); ?>" /><?php echo "n"; ?>
  96. <meta name="keywords" content="<?php echo rtrim($keywords,',');?>" /> <?php } ?>
  97. <?php if ( is_category() ) { ?>
  98. <meta name="description" content="<?php echo trim(strip_tags(category_description($cat_ID))); ?>" /><?php echo "n"; ?>
  99. <meta name="keywords" content="<?php echo trim(strip_tags(category_description($cat_ID))); ?>" />
  100. <?php } ?>
  101. <?php if ( is_tag() ) { ?>
  102. <meta name="description" content="<?php echo single_tag_title(); ?>" /> <?php } ?>
  103. <?php if ( is_home() ) { ?>
  104. <meta name="description" content="<?php echo get_option('swt_description'); ?>" /><?php echo "n"; ?>
  105. <meta name="keywords" content="<?php echo get_option('swt_keywords'); ?>" /> <?php } ?><?php echo "n"; ?>
  106. <!-- 狐狸影视城SEO优化代码 -->
  107. <?php echo "n"; ?>

 文章源自狐狸影视城-https://fox-studio.net/18782.html

Functions.php代码,添加分类和关键字文章源自狐狸影视城-https://fox-studio.net/18782.html

  1. //为WordPress页面添加标签和分类
  2. class PTCFP{
  3.   function __construct(){
  4.   add_action( 'init', array( $this, 'taxonomies_for_pages' ) );
  5.   /**
  6.   * 确保这些查询修改不会作用于管理后台,防止文章和页面混杂
  7.   */
  8.     if ( ! is_admin() ) {
  9.     add_action( 'pre_get_posts', array( $this, 'category_archives' ) );
  10.     add_action( 'pre_get_posts', array( $this, 'tags_archives' ) );
  11.     } // ! is_admin
  12.   } // __construct
  13.   /**
  14.   * 为“页面”添加“标签”和“分类”
  15.   *
  16.   * @uses register_taxonomy_for_object_type
  17.   */
  18.   function taxonomies_for_pages() {
  19.     register_taxonomy_for_object_type( 'post_tag', 'page' );
  20.     register_taxonomy_for_object_type( 'category', 'page' );
  21.   } // taxonomies_for_pages
  22.   /**
  23.   * 在标签存档中包含“页面”
  24.   */
  25.   function tags_archives( $wp_query ) {
  26.     if ( $wp_query->get( 'tag' ) )
  27.       $wp_query->set( 'post_type', 'any' );
  28.   } // tags_archives
  29.   /**
  30.   * 在分类存档中包含“页面”
  31.   */
  32.   function category_archives( $wp_query ) {
  33.     if ( $wp_query->get( 'category_name' ) || $wp_query->get( 'cat' ) )
  34.       $wp_query->set( 'post_type', 'any' );
  35.   } // category_archives
  36. // PTCFP
  37. $ptcfp = new PTCFP();
  38. ?>

发个帖子,网站似乎有在闹问题。卡住了,郁闷,这是要折腾我多久啊~~~文章源自狐狸影视城-https://fox-studio.net/18782.html

seo代码在附件下载,可以直接上传服务器,然后用include('目录/seo.php');来调用。文章源自狐狸影视城-https://fox-studio.net/18782.html

weinxin
千年骚狐
  • 本文由 发表于 2015年2月16日
  • 除非特殊声明,本站文章均为原创,转载请务必保留本文链接
评论:3   其中:访客  2   博主  1
    • 小易分享网
      小易分享网 2

      过来看看你的博客,希望每天都更新一些文章。

      • 风筝
        风筝 2

        你之前的没有加title标签么?那百度照样抓的出

      匿名

      发表评论

      匿名网友 填写信息

      :?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

      确定