WordPress内容页图片智能添加alt属性(自动添加文章标题作为图片alt属性)

前端之家收集整理的这篇文章主要介绍了WordPress内容页图片智能添加alt属性(自动添加文章标题作为图片alt属性)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

图片的alt属性网站seo有重要的作用,我们在使用wordpress建站添加文章页面时,很多时候会忘记添加图片的alt信息。对于有很多图片的网站,每张图片都要再去设置alt,也是一件很繁琐的事情。今天我们讲解一下给wordpress博客内容图片自动添加图片的alt属性方法

实现的原理:判断上传图片是否添加了alt属性,如果没有,会自动添加文章标题作为图片alt属性,以此来达到自动设置图片alt属性功能

切换到主题目录,打开functions.PHP文件添加如下代码

Crayon-5c891d4c78fbc839900918" class="Crayon-Syntax Crayon-theme-classic Crayon-font-monaco Crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouSEOver" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
Crayon-toolbar" data-settings=" mouSEOver overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">Crayon-title">functions.PHP
Crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
Crayon-button Crayon-nums-button" title="Toggle Line Numbers">
Crayon-button-icon">
Crayon-button Crayon-plain-button" title="Toggle Plain Code">
Crayon-button-icon">
Crayon-button Crayon-wrap-button" title="Toggle Line Wrap">
Crayon-button-icon">
Crayon-button Crayon-expand-button" title="Expand Code">
Crayon-button-icon">
Crayon-button Crayon-copy-button" title="Copy">
Crayon-button-icon">
Crayon-button Crayon-popup-button" title="Open Code In New Window">
Crayon-button-icon">
Crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
Crayon-plain-wrap">
Crayon-main" style="">Crayon-table">Crayon-row">
Crayon-nums " data-settings="show">
Crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
Crayon-num" data-line="Crayon-5c891d4c78fbc839900918-1">1
Crayon-num Crayon-striped-num" data-line="Crayon-5c891d4c78fbc839900918-2">2
Crayon-num" data-line="Crayon-5c891d4c78fbc839900918-3">3
Crayon-num Crayon-striped-num" data-line="Crayon-5c891d4c78fbc839900918-4">4
Crayon-num" data-line="Crayon-5c891d4c78fbc839900918-5">5
Crayon-num Crayon-striped-num" data-line="Crayon-5c891d4c78fbc839900918-6">6
Crayon-num" data-line="Crayon-5c891d4c78fbc839900918-7">7
Crayon-num Crayon-striped-num" data-line="Crayon-5c891d4c78fbc839900918-8">8
Crayon-num" data-line="Crayon-5c891d4c78fbc839900918-9">9
Crayon-num Crayon-striped-num" data-line="Crayon-5c891d4c78fbc839900918-10">10
Crayon-num" data-line="Crayon-5c891d4c78fbc839900918-11">11
Crayon-num Crayon-striped-num" data-line="Crayon-5c891d4c78fbc839900918-12">12
Crayon-num" data-line="Crayon-5c891d4c78fbc839900918-13">13
Crayon-num Crayon-striped-num" data-line="Crayon-5c891d4c78fbc839900918-14">14
Crayon-num" data-line="Crayon-5c891d4c78fbc839900918-15">15
Crayon-num Crayon-striped-num" data-line="Crayon-5c891d4c78fbc839900918-16">16
Crayon-num" data-line="Crayon-5c891d4c78fbc839900918-17">17
Crayon-num Crayon-striped-num" data-line="Crayon-5c891d4c78fbc839900918-18">18
Crayon-num" data-line="Crayon-5c891d4c78fbc839900918-19">19
Crayon-num Crayon-striped-num" data-line="Crayon-5c891d4c78fbc839900918-20">20
Crayon-num" data-line="Crayon-5c891d4c78fbc839900918-21">21
Crayon-num Crayon-striped-num" data-line="Crayon-5c891d4c78fbc839900918-22">22
Crayon-num" data-line="Crayon-5c891d4c78fbc839900918-23">23
Crayon-code">
Crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
Crayon-line" id="Crayon-5c891d4c78fbc839900918-1">Crayon-c">//图片添加alt属性
Crayon-line Crayon-striped-line" id="Crayon-5c891d4c78fbc839900918-2">Crayon-t">functionCrayon-h"> Crayon-e">image_altCrayon-sy">(Crayon-h"> Crayon-sy">$Crayon-i">imgaltCrayon-h"> Crayon-sy">)Crayon-sy">{
Crayon-line" id="Crayon-5c891d4c78fbc839900918-3">Crayon-h">Crayon-m">globalCrayon-h"> Crayon-sy">$Crayon-v">postCrayon-sy">;
Crayon-line Crayon-striped-line" id="Crayon-5c891d4c78fbc839900918-4">Crayon-h">Crayon-sy">$Crayon-v">titleCrayon-h"> Crayon-o">=Crayon-h"> Crayon-sy">$Crayon-v">postCrayon-v">post_titleCrayon-sy">;
Crayon-line" id="Crayon-5c891d4c78fbc839900918-5">Crayon-h">Crayon-sy">$Crayon-v">imgUrlCrayon-h"> Crayon-o">=Crayon-h"> Crayon-s">"]*src=(\"??)([^\" >]*?)\\1[^>]*>"Crayon-sy">;
Crayon-line Crayon-striped-line" id="Crayon-5c891d4c78fbc839900918-6">Crayon-h">Crayon-st">ifCrayon-sy">(Crayon-e">preg_match_allCrayon-sy">(Crayon-s">"/$imgUrl/siU"Crayon-sy">,Crayon-sy">$Crayon-v">imgaltCrayon-sy">,Crayon-sy">$Crayon-v">matchesCrayon-sy">,Crayon-v">PREG_SET_ORDERCrayon-sy">)Crayon-sy">)Crayon-sy">{
Crayon-line" id="Crayon-5c891d4c78fbc839900918-7">Crayon-h">Crayon-st">ifCrayon-sy">(Crayon-h"> Crayon-o">!Crayon-e">emptyCrayon-sy">(Crayon-sy">$Crayon-v">matchesCrayon-sy">)Crayon-h"> Crayon-sy">)Crayon-sy">{
Crayon-line Crayon-striped-line" id="Crayon-5c891d4c78fbc839900918-8">Crayon-h">Crayon-st">forCrayon-h"> Crayon-sy">(Crayon-sy">$Crayon-v">iCrayon-o">=Crayon-cn">0Crayon-sy">;Crayon-h"> Crayon-sy">$Crayon-v">iCrayon-h"> Crayon-o"><Crayon-h"> Crayon-e">countCrayon-sy">(Crayon-sy">$Crayon-v">matchesCrayon-sy">)Crayon-sy">;Crayon-h"> Crayon-sy">$Crayon-v">iCrayon-o">++Crayon-sy">)Crayon-sy">{
Crayon-line" id="Crayon-5c891d4c78fbc839900918-9">Crayon-h">Crayon-sy">$Crayon-v">tagCrayon-h"> Crayon-o">=Crayon-h"> Crayon-sy">$Crayon-v">urlCrayon-h"> Crayon-o">=Crayon-h"> Crayon-sy">$Crayon-v">matchesCrayon-sy">[Crayon-sy">$Crayon-v">iCrayon-sy">]Crayon-sy">[Crayon-cn">0Crayon-sy">]Crayon-sy">;
Crayon-line Crayon-striped-line" id="Crayon-5c891d4c78fbc839900918-10">Crayon-h">Crayon-sy">$Crayon-v">judgeCrayon-h"> Crayon-o">=Crayon-h"> Crayon-s">'/alt=/'Crayon-sy">;
Crayon-line" id="Crayon-5c891d4c78fbc839900918-11">Crayon-h">Crayon-e">preg_matchCrayon-sy">(Crayon-sy">$Crayon-v">judgeCrayon-sy">,Crayon-sy">$Crayon-v">tagCrayon-sy">,Crayon-sy">$Crayon-v">matchCrayon-sy">,Crayon-v">PREG_OFFSET_CAPTURECrayon-sy">)Crayon-sy">;
Crayon-line Crayon-striped-line" id="Crayon-5c891d4c78fbc839900918-12">Crayon-h">Crayon-st">ifCrayon-sy">(Crayon-h"> Crayon-e">countCrayon-sy">(Crayon-sy">$Crayon-v">matchCrayon-sy">)Crayon-h"> Crayon-o"><Crayon-h"> Crayon-cn">1Crayon-h"> Crayon-sy">)
Crayon-line" id="Crayon-5c891d4c78fbc839900918-13">Crayon-h">Crayon-sy">$Crayon-v">altURLCrayon-h"> Crayon-o">=Crayon-h"> Crayon-s">' alt="'Crayon-sy">.Crayon-sy">$Crayon-v">titleCrayon-sy">.Crayon-s">'" 'Crayon-sy">;
Crayon-line Crayon-striped-line" id="Crayon-5c891d4c78fbc839900918-14">Crayon-h">Crayon-sy">$Crayon-v">urlCrayon-h"> Crayon-o">=Crayon-h"> Crayon-e">rtrimCrayon-sy">(Crayon-sy">$Crayon-v">urlCrayon-sy">,'Crayon-sy">)Crayon-sy">;
Crayon-line" id="Crayon-5c891d4c78fbc839900918-15">Crayon-h">Crayon-sy">$Crayon-i">urlCrayon-h"> Crayon-sy">.Crayon-o">=Crayon-h"> Crayon-sy">$Crayon-v">altURLCrayon-sy">.'Crayon-sy">;
Crayon-line Crayon-striped-line" id="Crayon-5c891d4c78fbc839900918-16">Crayon-h">Crayon-sy">$Crayon-v">imgaltCrayon-h"> Crayon-o">=Crayon-h"> Crayon-e">str_replaceCrayon-sy">(Crayon-sy">$Crayon-v">tagCrayon-sy">,Crayon-sy">$Crayon-v">urlCrayon-sy">,Crayon-sy">$Crayon-v">imgaltCrayon-sy">)Crayon-sy">;
Crayon-line" id="Crayon-5c891d4c78fbc839900918-17">Crayon-h">Crayon-sy">}
Crayon-line Crayon-striped-line" id="Crayon-5c891d4c78fbc839900918-18">Crayon-h">Crayon-sy">}
Crayon-line" id="Crayon-5c891d4c78fbc839900918-19">Crayon-h">Crayon-sy">}
Crayon-line Crayon-striped-line" id="Crayon-5c891d4c78fbc839900918-20">Crayon-h">Crayon-st">returnCrayon-h"> Crayon-sy">$Crayon-v">imgaltCrayon-sy">;
Crayon-line" id="Crayon-5c891d4c78fbc839900918-21">Crayon-sy">}
Crayon-line Crayon-striped-line" id="Crayon-5c891d4c78fbc839900918-22">
Crayon-line" id="Crayon-5c891d4c78fbc839900918-23">Crayon-e">add_filterCrayon-sy">(Crayon-h"> Crayon-s">'the_content'Crayon-sy">,Crayon-s">'image_alt'Crayon-sy">)Crayon-sy">;

原文链接:https://www.f2er.com/wordpress/238509.html

猜你在找的wordpress相关文章