zoukankan      html  css  js  c++  java
  • 设置WordPress文章关键词自动获取,文章所属分类名称,描述自动获取文章内容,给文章的图片自动加上AlT标签

    最近在优化网站,SEO优化标准:每一篇文章都要有关键词,关键词的个数为3到6个。每一篇文章都要有描述,描述的字数为汉字在70~80之间,在160个字符之间。
    每一篇文章的图片都要有Alt标签,自动给图片加上Alt标签
    实现代码:
    1.自动获取关键词:关键词组成结构为:文章所属父级分类名称(category_parent),所属分类名称(category),文章标题(title)
    把以下代码写入functions.php文件中:

    //关键字
    function deel_keywords() {
      global $s, $post;//声明$post全局变量
      $keywords = '';
      if ( is_single() ) {
    	//if ( get_the_tags( $post->ID ) ) {
    	  //foreach ( get_the_tags( $post->ID ) as $tag ) $keywords .= $tag->name . ', ';
    	//}
    //如果文章有标签,关键词为标签 $category = get_the_category(); $parent = get_cat_name($category[0]->category_parent); //echo $parent;//得到父级分类名称 foreach ( get_the_category( $post->ID ) as $category ) $keywords .=$parent.','. $category->cat_name . ', '.get_the_title(); //关键词为父级分类名称,分类名称,文章标题 //下面判断条件为不同模板 $keywords = substr_replace( $keywords , '' , -2); }// elseif ( is_home () ) { $keywords = dopt('d_keywords'); // } elseif ( is_tag() ) { $keywords = single_tag_title('', false); //} elseif ( is_category() ) { $keywords = single_cat_title('', false); //} elseif ( is_search() ) { $keywords = esc_html( $s, 1 ); // } else { $keywords = trim( wp_title('', false) ); // } if ( $keywords ) { echo "<meta name="keywords" content="$keywords"> "; } } //关键字加入head头部代码 add_action('wp_head','deel_keywords');

    2.自动获取文章内容作为描述,少于160个字符  

    //网站描述
    function deel_description() {
      global $s, $post;
      $description = '';
     // $blog_name = get_bloginfo('name');
      if ( is_singular() ) {
    	//if( !empty( $post->post_excerpt ) ) {
    	//  $text = $post->post_excerpt;
            //此处判断是否有摘要,有摘要,将其作为描述
    	//} else {
    	  $text = $post->post_content;
    	}
    	$description = trim( str_replace( array( "
    ", "
    ", "
    ", " ", " "), " ", str_replace( """, "'", strip_tags( $text ) ) ) );
        //以下为各种判断条件
    	//if ( !( $description ) ) $description = $blog_name . "-" . trim( wp_title('', false) );
     // } elseif ( is_home () )    { $description = dopt('d_description'); // 首頁要自己加
     // } elseif ( is_tag() )      { $description = $blog_name . "'" . single_tag_title('', false) . "'";
    //  } elseif ( is_category() ) { $description = trim(strip_tags(category_description()));
    //  } elseif ( is_archive() )  { $description = $blog_name . "'" . trim( wp_title('', false) ) . "'";
    //  } elseif ( is_search() )   { $description = $blog_name . ": '" . esc_html( $s, 1 ) . "' 的搜索結果";
     // } else { $description = $blog_name . "'" . trim( wp_title('', false) ) . "'";
     //}
      $description = mb_substr( $description, 0, 80, 'utf-8' );
      echo "<meta name="description" content="$description">
    ";
      }
    //页面描述加入头部head标签中
    add_action('wp_head','deel_description');
    

    3.自动给文章的图片加上ALT属性 (把以下代码放到single.php文件中) 

    <script type="text/javascript">
    			var title = "<?php the_title(); ?>"+'-走势分析图';
    			$(function(){
    				$('.article-content img').attr('alt',title);
    			})
    </script>
    //其中.article-content img,要获取到图片所在div
    

    以上是自动对文章内容进行SEO优化,针对wp的页面SEO,建议使用ALL in one pack插件,可以实现自定义编辑。  

      

  • 相关阅读:
    20155318Java课堂实践20170510
    2017-2018-1 20155220 20155309 20155317 实验一 开发环境的熟悉
    2017-2018 20155309南皓芯 信息安全系统基础设计第五周博客
    2017-2018 20155309南皓芯 信息安全系统基础设计第四周博客
    20155309 南皓芯 信息安全系统基础设计第三周博客
    关于利用GPG加解密的实验
    20155309南皓芯第二周课堂实践
    2017-2018 20155309南皓芯 信息安全系统基础设计第一周博客
    20155309 2016-2017-2《Java程序设计》课程总结
    20155309实验五 网络编程与安全
  • 原文地址:https://www.cnblogs.com/cheryshi/p/7723317.html
Copyright © 2011-2022 走看看