主机评测网,专注vps、独立服务器等主机评测
最专业的主机评测网站

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

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

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://vps.caogenba.com.com/86359.html

赞(0) 打赏
未经允许不得转载:主机测评 » 使用标题标签作为WordPress文章图片的替换(WordPress原始标签插件)

评论 抢沙发

登录

找回密码

注册