WordPress文章中的图片添加alt属性,很多时候都是因为SEO,但很多人包括小编在内,很多时候也是不会去添加的。
相关阅读:
1、为WordPress文章图片自动添加alt与title属性
2、WordPress图片怎么自动添加alt和title属性
今天追格小编教你如何将title标签作为文章图片的alt属性。
function callback($buffer) {
/* modify buffer here, and then return the updated code*/
$title='';
$res = preg_match('/<title>(.*?)</title>/', $buffer, $title_matches);
if ($res) {
/*Clean up title: remove EOL's and excessive whitespace.*/
$title = preg_replace('/s+/', ' ', $title_matches[1]);
$title = trim($title);
}
preg_match_all('/<img (.*?)/>/', $buffer, $images);
if(!is_null($images)) {
foreach($images[1] as $index => $value) {
preg_match('/alt="(.*?)"/', $value, $img);
preg_match('/alt='(.*?)'/', $value, $img2);
if(!is_null($images)) {
if((!isset($img[1]) || $img[1] == '') || (!isset($img2[1]) || $img2[1] == '')) {
$new_img = str_replace('<img', '<img alt="'.$title.'"', $images[0][$index]);
$buffer = str_replace($images[0][$index], $new_img, $buffer);
}
}
}
}
return $buffer;
}
function buffer_start() {
ob_start();
}
function buffer_end() {
echo callback(ob_get_clean());
}
add_action('wp', 'buffer_start', 0); add_action('wp_footer', 'buffer_end');
上面代码添加到当前WordPress模板的functions.php文件中即可。
需要注意的是,上面代码中虽有缓冲区代码,可能还会降低效率,建议安装静态缓存插件。
-
WordPress圈子到底怎么了...话说,国内WordPress圈子是否还有新的发展机遇?这个充斥着同质化产品和激烈内卷的市场中,如何在价格和功能的差异化上寻求突破,为圈子注入新的活力,是开发者不得不面对的问题。
-
重塑WordPress生态的思考与探索前两天的《WordPress圈子到底怎么了...》这篇文章大家都挺有感触的,群里聊得热火朝天。里头啥声音都有:有的哥们儿直接吐槽WordPress性能拖后腿,躲都躲不及;有的则感慨流量难搞,但坚持建站纯粹是出于热爱和那份情怀;还有人预言,以后建站啊,模块化、可视化才是王道;较热闹的还得数短视频和直播,这家伙一来,内容消费整个儿变了天,网站和小程序怎么赚钱都成难题了。
-
WordPress企业主题近期,国内WordPress社群似乎进入了一段相对宁静的时期,无论是活跃的用户群体还是勤勉的开发者们,都呈现出一种沉稳的态势。在这样一个略显静谧的时刻,追格团队选择利用这段时间沉潜下来,专心致志地精进自身的产品,力求在每一个细节上都追求完善,以备在不久的将来,以更加超凡脱俗的面貌回馈给广大用户与开发者。
-
Loco Translate 一款专为WordPress设计的翻译插件WordPress Loco Translate是一款专为WordPress用户设计的翻译插件,它简化了主题和插件的多语言翻译过程。
-
追格小程序V2.5.0更新发布追格小程序V2.5.0版本的更新,我们没给大家添太多新花样,主要是把“内功”练好了。我们认真检查了上一版里那些组件之间的小摩擦,一点点地调整、优化,搞定了超过40多个小地方。这样一来,小程序跑起来就更顺畅了,大家用起来也会感觉更舒服、更顺手。这次虽然没大张旗鼓,但确实是让产品站得更稳、更可靠了。
-
WordPress后台待审文章显示气泡提示的方法之前小编分享过一篇《WordPress 后台菜单添加 badge-红点提示(气泡通知)》的文章,今天我简单说说待审文章(为全部文章类型显示待审角标提示)的气泡提示如何操作。
暂无评论,抢个沙发...