设置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\">\n";} } //关键字加入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( "\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文件中) 

<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插件,可以实现自定义编辑。  

 

  

转载于:https://www.cnblogs.com/cheryshi/p/7723317.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/415497.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

优化JMeter自身性能

JMeter负载测试时使用GUI界面和较多的收集测试结果的监听器容易造成jmeter的性能瓶颈&#xff0c;远程测试时的控制台尤为明显。提升JMeter负载测试时性能的方法如下&#xff1a; 官方的解决办法&#xff1a; http://jakarta.apache.org/jmeter/usermanual/best-practices.html…

centos 宝塔面板 mongodb 给新的数据库 创建登录用户和密码

之前写过一篇冷环境下mongodb从零开始创建用户密码的文章。 https://blog.csdn.net/solocao/article/details/83347658 当我们需要再新增一个账号时&#xff0c;用下面的方法&#xff0c;和第一篇大同小异&#xff1a; 1、进入mongodb安装目录&#xff0c;下面是宝塔面板的默…

EventBus使用实例,观察者模式

依赖 compile org.greenrobot:eventbus:3.0.0 构造函数方法类(用于传递参数) public class ChangeFragmentEvent {private int viewId;private String ass;public ChangeFragmentEvent(int id,String ss) {this.viewId id;this.assss;}public int getViewId() {return viewId…

前端学习(2630):git安装包下载

下载git时&#xff0c;先进官网看 https://git-scm.com/download/win 然后发现几kb的网速&#xff0c;这是要让我下一年么&#xff0c;找了找网上有没有其他的镜像&#xff0c;发现阿里有一个镜像&#xff0c;下载速度超快。 网址&#xff1a;https://npm.taobao.org/mirror…

coding ssh_exchange_identification: Connection closed by remote host

首先&#xff0c;强力吐槽coding.net。前段时间突然提交不上代码了。老是报错&#xff0c;一怒之下删除.ssh&#xff0c;然后重新根据官方文档配置&#xff0c;我是买的企业版&#xff0c;是付费的。 于是官方文档坑了 https://e.coding.net/help/git/ssh-key/#i-3 感觉这文档…

jmeter模拟http请求/发送gzip数据

jmeter中get请求gzip数据的方法&#xff1a; 在jmeter线程组中添加“http信息头管理器”&#xff0c;并添加名称&#xff1a;Accept-Encoding值: gzip,deflate 注&#xff1a;HTTP信息头Accept-Encoding 是客户端发给服务器,声明客户端支持的编码类型 验证服务器是否返回了gzip…

Java randomString

public static String randomString(int strLength) {Random rnd ThreadLocalRandom.current();StringBuilder ret new StringBuilder();for (int i 0; i < strLength; i) {boolean isChar (rnd.nextInt(2) % 2 0);// 输出字母还是数字if (isChar) { // 字符串int choi…

Service Intent must be explicit

隐性调用需要设置包名 serviceIntent new Intent("ce"); serviceIntent.setPackage("clan.yuanxin.com.myditukaifa");//设置包名 startService(serviceIntent);

前端学习(2631):git安装

git 提交 全部文件 1、git add . git add xx 命令可以将xx文件添加到暂存区&#xff0c;如果有很多改动可以通过get add -A .来一次添加所有改变的文件。注意 -A 选项后面还有一个句点。git add -A 表示添加所有内容&#xff0c;git add . 表示添加新文件和编辑过的文件不包括…

宝塔 windows 2012 配置 ftp 允许 fileZilla 连接

使用fileZilla连接宝塔 windows 2012的ftp遇到了一些坑&#xff0c;下面记录一下步骤。 1、当安装完全新当宝塔 windows 2012后&#xff0c;首先配置ftp 账号&#xff1a; 2、在阿里云上配置安全组规则&#xff0c;开放端口 21 22 80 3306 39000/40000 3、在fileZilla进行配置…

jmeter语言设置

Jmeter启动时通过JVM判断界面显示语言&#xff0c;正常情况可以显示正确的语言&#xff0c;但如果显示错误或者喜欢英文环境&#xff0c;就需要自己通过菜单或者自己配置jmeter.properties设置语言。 jmeter.properties中配置Jmeter界面语言 \apache-jmeter-2.9\bin\jmeter.pro…

带参数的光标

转载于:https://www.cnblogs.com/myfaith-feng/p/7725813.html

如何解决360的导航一直绑定为首页(亲测有效)

1首先360导航后面的属性一定被追加了网址 你删除会显示权限不足 2打开文件目录 从新生成快捷方式u即可 难呀 亲测有效

android.os.BinderProxy cannot be cast to

Android在绑定service的时候出现java.lang.ClassCastException&#xff1a;android.os.BinderProxy cannot be cast to ...异常时&#xff1a;修改manifest文件里边相关服务去掉android:process":remote"。

宝塔面板 windows 2012 mysql 允许远程连接

一开始配置windows 2012 mysql连接的时候&#xff0c;怎么连&#xff0c;都连不上。后台知道需要配置一个参数就OK。 1、找到需要修改文件my.ini的目录 打开my.ini&#xff0c;在[mysqld]下添加skip-grant-tables&#xff0c; 2、关闭MySQL服务并重新开启&#xff0c;远程就…

jmeter测试TCP服务器/模拟发送TCP请求

jmeter测试TCP服务器&#xff0c;使用TCP采样器模拟发送TCP请求。 TCP采样器&#xff1a;打开一个到指定服务器的TCP / IP连接&#xff0c;然后发送指定文本并等待响应。 jmeter模拟发送TCP请求的方法&#xff1a; 1.新建线程组&#xff0c;然后在线程组中新建TCP采样器 TCP采样…

转:Fiddler抓包工具总结

http://www.cnblogs.com/yyhh/p/5140852.html#l02转载于:https://www.cnblogs.com/Reclouds-shangri-la/p/7728726.html

mongoose更新数据,如果这条记录不存在,则直接变为新增

主要用到findOneAndUpdate let query { /* query */ }; let update {expire: new Date()}; let options {upsert: true, new: true, setDefaultsOnInsert: true}; let model await Model.findOneAndUpdate(query, update, options);