VPS参考、测评、推荐
分享你关注的VPS主机优惠信息

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

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

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

使用标题标签作为WordPress文章图片的替换(WordPress原始标签插件)将下面的添加到当前的主题功能模板functions.中。

函数回调($buffer) { /*在这里修改buffer,然后返回后的(.*?)& lt/title & ;/’,$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 _ ($ 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(‘ ‘,’ buffer_start ‘,0);add_action(‘wp_footer ‘,’ buffer _ end ‘);虽然在代码中添加了缓冲区,但还是会降低效率。建议静态缓存插件。

其他方法:

函数img _ alt($ content){ global $ post;preg _ match _ all(‘/& lt;img(。*?)/& gt;/’,$content,$ images);如果(!is _ ($ 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://.caogenba.com.com/86359.html

赞(0) 打赏
未经允许不得转载:草根吧VPS_最新VPS信息参考 » 使用标题标签作为WordPress文章图片的替换(WordPress原始标签插件)
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址