格子鸽子 發表於 2023-10-26 00:00:00

织梦dedecms5.7版自动给图片添加alt属性的方法

<p><span style="font-size: 14px;">搜素引擎看不懂图片,通过给图片添加alt属性,有助于搜索引擎判了解这是一张什么图片,利于搜索引擎优化,在seo优化中,图片ALT属性描述很重要,图片alt属性就是对图片进行描述的文字。描述中可以加入网站的关键词,来增加关键词密度,当搜索引擎来爬取页面的时候,图片的描述也可以更好的帮助搜索引擎对图片进行理解,利于网站的排名。做好了图片的alt属性无论是对搜索引擎的爬取还是用户体验都是有利无害的,所以我们一定要重视图片alt属性。   织梦本身没有批量添加或自动替换图片alt描述的功能,而使用织梦建站的朋友都有很多采集来的文章,大部分图片都没有alt属性,非常不利于SEO优化,手工添加又太费时费力,怎么办呢?下面就以织梦5.7为例介绍下怎么给你网站所有文章里的图片自动添加ALT属性,其他版本应该也可以,网上这一类的教程很多,网友在5.3/5.5版测试通过,5.6没试过,本文教程在织梦最新版5.7测试通过:织梦5.7版自动给图片添加alt属性具体解决方法: 一. 找到网站Include目录下的arc.archives.class.php 文件并打开  (模板标签:{dede:field.body/} ,如果是其它字段,可以修改$this-&gt;Fields[&#39;body&#39;] 为其它的字段名。) 二. 用组合键 Ctrl+F 查找下面的这段代码</span></p><p class="msgborder" style="margin: 0px auto 3px; padding: 0px 3px; outline: none; line-height: 25.2px; font-size: 14px; clear: both; border-right: 1px solid rgb(0, 153, 204); background: rgb(221, 237, 251); overflow: hidden; border-left: 1px solid rgb(0, 153, 204); word-break: break-all; border-bottom: 1px solid rgb(0, 153, 204); word-wrap: break-word; ">&nbsp; &nbsp;
        //设置全局环境变量 $this-&gt;Fields[&#39;typename&#39;] = $this-&gt;TypeLink-&gt;TypeInfos[&#39;typename&#39;]; SetSysEnv($this-&gt;Fields[&#39;typeid&#39;],$this-&gt;Fields[&#39;typename&#39;],$this-&gt;Fields[&#39;aid&#39;],$this-&gt;Fields[&#39;title&#39;],&#39;archives&#39;);</p><p><span style="font-size: 14px;">三. 在以上代码下面添加以下代码即可</span></p><p class="msgborder" style="margin: 0px auto 3px; padding: 0px 3px; outline: none; line-height: 25.2px; font-size: 14px; clear: both; border-right: 1px solid rgb(0, 153, 204); background: rgb(221, 237, 251); overflow: hidden; border-left: 1px solid rgb(0, 153, 204); word-break: break-all; border-bottom: 1px solid rgb(0, 153, 204); word-wrap: break-word;">&nbsp; &nbsp;
        //替换图片Alt为文档标题 $this-&gt;Fields[&#39;body&#39;] = str_ireplace(array(&#39;altalt=&quot;&quot;&#39;,&#39;alt=&#39;&#39;&#39;),&#39;&#39;,$this-&gt;Fields[&#39;body&#39;]); $this-&gt;Fields[&#39;body&#39;] = preg_replace(&quot;@ {0,}alt{0,}=[&quot;&#39;s]{0,}{0,}[&quot;&#39;s] @isU&quot;,&quot; &quot;,$this-&gt;Fields[&#39;body&#39;]); $this-&gt;Fields[&#39;body&#39;] = str_ireplace(&quot;<img alt="" this-=""/>Fields[&quot; /&gt;Fields[&#39;title&#39;].&quot;&#39;</p><p><span style="font-size: 14px;">如果文章标题是 “新手如何推广网站” 则生成后的文章内图片的img属性就是 ,我将其改为了下面的这段代码,使其实现图片ALT属性样式为 alt=”新手如何推广网站_jb51.net” ,这里的 _jb51.net 可以用你网站的主推关键词或网址来替换。</span></p><blockquote><ol class="dp-xml list-paddingleft-2"><li><span class="tag">&lt;</span><span class="tag-name">img</span>&nbsp;<span class="attribute">alt</span>=&quot;&quot;<span class="attribute-value">&quot;.$this-&quot;</span>&nbsp;<span class="tag">/&gt;</span>Fields[&#39;title&#39;].&quot;_www.jb51.net&quot;&nbsp;</li></ol></blockquote><p><span style="font-size: 14px;">这里是alt属性的替换规则,默认是替换为文章的标题,稍微转换下思路,其实也可以用tag标签或别的文章属性来代替。 四. 由于使用了正则处理,会降低一些生成速度,如果数据量小,则可以忽略。</span></p>
頁: [1]
查看完整版本: 织梦dedecms5.7版自动给图片添加alt属性的方法