在最近的优化网站中,SEO 优化标准:每篇文章必须有关键词,关键词的数量是 3 到 6 个。每篇文章都要有描述。描述的字数在 70 到 80 个字符之间,160 个字符之间。
文章中的每一张图片都必须有一个 Alt 标签,并且自动给图片添加 Alt 标签。
实现代码:
1.自动获取关键词:关键词的构成结构为:父类别名(category_parent)、类别名(category)和文章标题。
将以下代码写入functions.php文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | //关键字 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 . ', '; //}<br> //如果文章有标签,关键词为标签 $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\">\n" ; } } //关键字加入 head 头部代码 add_action( 'wp_head' , 'deel_keywords' ); |
2.自动获取文章内容作为描述,少于 160 个字符
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | //网站描述 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 ( "\r\n" , "\r" , "\n" , " " , " " ), " " , 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\">\n" ; } //页面描述加入头部 head 标签中 add_action( 'wp_head' , 'deel_description' ); |
3.自动给文章的图片加上 ALT 属性 (把以下代码放到 single.php 文件中)
1 2 3 4 5 6 7 | <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 插件,可以实现自定义编辑。
转载请注明:汇站网 » 设置 WordPress 文章关键词自动获取、文章分类名称、描述自动获取文章内容,并自动给文章图片添加 AlT 标签。
汇站网 WordPress教程 设置WordPress文章关键词自动获取、文章分类名称、描述自动获取文章内容,并自动给文章图片添加AlT标签。 https://www.huizhanii.com/19512.html
汇站
站长资源下载中心-找源码上汇站
常见问题
- 本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。
相关文章
猜你喜欢
- 如果WordPress搜索结果为空,自动跳转到网站首页 2024-02-01
- 浏览器缓存可以分为强缓存和协商缓存两种方式 2024-01-21
- 如何让WordPress支持上传WebP格式图片? 2024-01-20
- 利用WordPress设置API实现自定义设置页面。 2024-01-20
- WordPress api接口 版本历史 2024-01-20
- WordPress用户密码加密和验证的详细解释 2024-01-19
- WordPress禁用媒体附件页面教程 2024-01-19
- WordPress修改了文章的默认密码保护提示 2024-01-19
- WordPress配置(站群)多站点详细教程方法 2024-01-19
- WordPress如何用指定图片自动替换文章图片 2024-01-17