自动为wordpress文章图片添加alt属性和title属性

IT资讯
IT资讯
IT资讯
1167
文章
257
评论
2020-12-3022:30:47
评论
2,180 842字

这个技巧,相信大家也都是很期盼的,会省心省力很多!

其实很简单了,看一下就能看懂吧:

 

/**
*自动为wordpress文章图片添加alt属性和title属性
*/
function image_alttitle( $imgalttitle ){
global $post;
$category = get_the_category();
$flname=$category[0]->cat_name;
$btitle = get_bloginfo();
$imgtitle = $post->post_title;
$imgUrl = "<img\s[^>]*src=(\"??)([^\" ]*?)\\1[^>]";
if(preg_match_all("/$imgUrl/siU",$imgalttitle,$matches,PREG_SET_ORDER)){
if( !empty($matches) ){
for ($i=0; $i < count($matches); $i++){
$tag = $url = $matches[$i][0];
$j=$i+1;
$judge = '/title=/';
preg_match($judge,$tag,$match,PREG_OFFSET_CAPTURE);
if( count($match) < 1 ) $altURL = ' alt="'.$imgtitle.' '.$flname.' 第'.$j.'张-'.$btitle.'" title="'.$imgtitle.' '.$flname.' 第'.$j.'张-'.$btitle.'" ';
$url = rtrim($url.'');
$url .=  $altURL.'';
$imgalttitle = str_replace($tag,$url,$imgalttitle);
}
}
}
return $imgalttitle;
}
add_filter( 'the_content','image_alttitle');

 

 

 

 

 

 

  • Copyright ©  PC在线云端  版权所有.
  • 转载请务必保留本文链接:https://www.gaofumei.net/web/10345.html
WordPress 使用 Github & JsDelivr 加速静态文件 WEB技术

WordPress 使用 Github & JsDelivr 加速静态文件

一个网站的打开速度至关重要,会直接影响搜索引擎排名和用户体验。如果您的网站加载越慢,用户离去的就会越快,而用户又是我们生存的根本。   当然网站加载速度受很多因素影响,今天我们使用 Gith...
Orgorg速率最高可达1000Mbps,流畅观看Youtube 4K、TikTok,支持 Windows、Android、iOS、Mac,支持 微信、支付宝 付款!
超高性价比,新用户注册送1元,可直接在商店购买套餐。节点和负载服务器很多,稳定性和速度有保障。有共享流媒体会员。有三端VPN程序。最萌の云 ☁️顶级云服务商,超过50个G口节点,上百台服务器负载加速,稳且快,高峰秒8K。提供网飞等会员共享,超值!限时免费尝鲜!
全球数据中心,多点BGP保证速度,无视晚高峰,全天4K秒开,IPLC专线无惧封锁
全IEPL /青云跨境,高峰时期稳定8K播放,流媒体影视, ChatGPT 解锁保障,客户端无日志保护您的隐私安全,稳定运行5年+
匿名

发表评论

匿名网友 填写信息

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