VPS参考测评推荐
专注分享VPS主机优惠信息
衡天云优惠活动
华纳云最新优惠促销活动
jtti最新优惠促销活动

使用标题标签作为WordPress文章图片的替换(WordPress原始标签插件)

locvps
主机参考:VPS测评参考推荐/专注分享VPS服务器优惠信息!若您是商家可以在本站进行投稿,查看详情!此外我们还提供软文收录、PayPal代付、广告赞助等服务,查看详情!
我们发布的部分优惠活动文章可能存在时效性,购买时建议在本站搜索商家名称可查看相关文章充分了解该商家!若非中文页面可使用Edge浏览器同步翻译!PayPal代付/收录合作

WordPress站长在发布文章时往往不注意给图片添加描述(ALT),导致文章中大量图片缺少ALT属性,不利于SEO。网上有很多自动给文章和图片添加ALT属性的教程。这里有一个国外的方法供参考。

使用标题标签作为WordPress文章图片的替换(WordPress原始标签插件)-主机参考WordPress只需要将下面的代码添加到当前的主题功能模板functions.php中。

函数回调($buffer) { /*在这里修改buffer,然后返回更新后的代码*/$ title = ' ';$ RES = preg _ match('/& lt;title & gt(.*?)& lt/title & gt;/',$buffer,$ title _ matches);if($ RES){/*清理标题:删除EOL和过多的空白。*/ $title = preg_replace('/s+/',' ',$ title _ matches & # 0911]);$ title = trim($ title);} preg _ match _ all('/& lt;img(。*?)/& gt;/',$buffer,$ images);如果(!is _ null($ images)){ foreach($ images & # 091;1] as $index = >$value) { preg_match('/alt= "(。*?)"/',$value,$ img);preg_match('/alt= '(。*?)'/',$value,$ img 2);如果(!is_null($images)) { if((!isset($ img & # 091;1])| | $ img & # 091;1] == '') || (!isset($ img 2 & # 091;1])| | $ img 2 & # 091;1]= = ' '){ $ new _ img = str _ replace(' & lt;img ',' & ltimg alt= " "。$title。“‘,$ images & # 0910][$ index]);$ buffer = str _ replace($ images & # 091;0][$index],$new_img,$ buffer);} } } }返回$ buffer}函数buffer _ start(){ ob _ start();}函数buffer_end() { echo回调(ob _ get _ clean());} add_action('wp ',' buffer_start ',0);add_action('wp_footer ',' buffer _ end ');虽然在代码中添加了缓冲区,但还是会降低效率。建议安装静态缓存插件。

其他方法:

函数img _ alt($ content){ global $ post;preg _ match _ all('/& lt;img(。*?)/& gt;/',$content,$ images);如果(!is _ null($ images)){ foreach($ images & # 091;1] as $index = >$ value){ $ new _ img = str _ replace(' & lt;img ',' & ltimg alt= " "。get_the_title()。'-'.get_bloginfo('name '),'“标题=”。get_the_title()。'-'.get_bloginfo('name '),'“‘,$ images & # 0910][$ index]);$ content = str _ replace($ images & # 091;0][$index],$new_img,$ content);} } return $ content} add_filter('the_content ',' img_alt ',99999);

这几篇文章你可能也喜欢:

本文由主机参考刊发,转载请注明:使用标题标签作为WordPress文章图片的替换(WordPress原始标签插件) https://zhujicankao.com/86359.html

【腾讯云】领8888元采购礼包,抢爆款云服务器 每月 9元起,个人开发者加享折上折!
打赏
转载请注明原文链接:主机参考 » 使用标题标签作为WordPress文章图片的替换(WordPress原始标签插件)
主机参考仅做资料收集,不对商家任何信息及交易做信用担保,购买前请注意风险,有交易纠纷请自行解决!请查阅:特别声明

评论 抢沙发

评论前必须登录!