在百度搜索引擎优化指南中明确了一个网的图片alt是很重要的一部分,做好图片的seo关键字alt属性是至关重要的!
一个网站想要更好的适应搜索引擎的话,就要把最基础的一些小优化标签做好,虽然说现在搜索都很厉害能够识别图片,但是除非的你的图片每一张都是像成龙、李连杰等知名图片。。。
不然你还是要给你自己的图添加alt标签能搜索引擎去识别的。整个网站那么多帖子图片,我们不可能一个个去添加吧。现在交易国Discuz小编就教你全站帖子图片自动添加alt标签的方法。
下面小编就以Discuz!X3.2为例(正确的方法,全文总共有两处是需要修改替换的,有两处是需要添加的):
首先我们找到:/template/你的模板/forum/discuzcode.htm文件,如果你使用的模板没有这个文件,找不到没有是正常的。只是有些特别的会有。没有的话就去默认的模板下找:/template/default/forum/discuzcode.htm文件
一、修改替换(仔细看,替换2处)(大概在194行到200行)
代码不是很好找,所以直接搜索 例如: <div class="mbn savephotop"> ,对比下面代码:
<div class="mbn savephotop">
<!--{if $_G['setting']['thumbstatus'] && $attach['thumb']}-->
<a href="javascript:;"><img id="aimg_$attach[aid]" aid="$attach[aid]" src="{STATICURL}image/common/none.gif" onclick="zoom(this, this.getAttribute('zoomfile'), 0, 0, '{$_G[setting][showexif]}')" zoomfile="{if $attach[refcheck]}forum.php?mod=attachment{$is_archive}&aid=$aidencode&noupdate=yes¬humb=yes{else}{$attach[url]}$attach[attachment]{/if}" file="{if $attach[refcheck]}forum.php?mod=attachment{$is_archive}&aid=$aidencode{else}{$attach[url]}$attachthumb{/if}" alt="$attach[imgalt]" title="$attach[imgalt]" w="$attach[width]" /></a>
<!--{else}-->
<img id="aimg_$attach[aid]" aid="$attach[aid]" src="{STATICURL}image/common/none.gif" zoomfile="{if $attach[refcheck]}forum.php?mod=attachment{$is_archive}&aid=$aidencode&noupdate=yes¬humb=yes{else}{$attach[url]}$attach[attachment]{/if}" file="{if $attach[refcheck]}forum.php?mod=attachment{$is_archive}&aid=$aidencode&noupdate=yes{else}{$attach[url]}$attach[attachment]{/if}" $widthcode alt="$attach[imgalt]" title="$attach[imgalt]" w="$attach[width]" />
<!--{/if}-->
</div>
修改:
<div class="mbn savephotop">
<!--{if $_G['setting']['thumbstatus'] && $attach['thumb']}-->
<a href="javascript:;"><img id="aimg_$attach[aid]" aid="$attach[aid]" src="{STATICURL}image/common/none.gif" onclick="zoom(this, this.getAttribute('zoomfile'), 0, 0, '{$_G[setting][showexif]}')" zoomfile="{if $attach[refcheck]}forum.php?mod=attachment{$is_archive}&aid=$aidencode&noupdate=yes¬humb=yes{else}{$attach[url]}$attach[attachment]{/if}" file="{if $attach[refcheck]}forum.php?mod=attachment{$is_archive}&aid=$aidencode{else}{$attach[url]}$attachthumb{/if}" alt="$_G[forum_thread][subject]- $_G[setting][sitename]" title="$_G[forum_thread][subject]- $_G[setting][sitename]" w="$attach[width]" /></a>
<!--{else}-->
<img id="aimg_$attach[aid]" aid="$attach[aid]" src="{STATICURL}image/common/none.gif" zoomfile="{if $attach[refcheck]}forum.php?mod=attachment{$is_archive}&aid=$aidencode&noupdate=yes¬humb=yes{else}{$attach[url]}$attach[attachment]{/if}" file="{if $attach[refcheck]}forum.php?mod=attachment{$is_archive}&aid=$aidencode&noupdate=yes{else}{$attach[url]}$attach[attachment]{/if}" $widthcode alt="$_G[forum_thread][subject]- $_G[setting][sitename]" title="$_G[forum_thread][subject]- $_G[setting][sitename]" w="$attach[width]" />
<!--{/if}-->
</div>
其实就是把代码中的两处
alt="$attach[imgalt]" title="$attach[imgalt]"
修改成:
alt="$_G[forum_thread][subject]- $_G[setting][sitename]" title="$_G[forum_thread][subject]- $_G[setting][sitename]"
提示: $_G[forum_thread][subject]- $_G[setting][sitename] 这个标签的意思是 帖子标题-网站名称 的意思,如果你不想要网站名称的带横杠删掉后面的就可以。
二、添加代码(仔细看,添加2处)(大概在257行到261行)
代码不是很好找,所以直接搜索 例如: <!--{if $_G['setting']['thumbstatus'] && $attach['thumb']}--> ,对比下面代码
<!--{if $_G['setting']['thumbstatus'] && $attach['thumb']}-->
<img{if $attach['price'] && $_G['forum_attachmentdown'] && $_G['uid'] != $attach['uid']} class="attprice"{/if} style="cursor:pointer" id="aimg_$attach[aid]" aid="$attach[aid]" src="{STATICURL}image/common/none.gif" onclick="zoom(this, this.getAttribute('zoomfile'), 0, 0, '{$_G[setting][showexif]}')" zoomfile="{if $attach[refcheck]}forum.php?mod=attachment{$is_archive}&aid=$aidencode&noupdate=yes¬humb=yes{else}{$attach[url]}$attach[attachment]{/if}" file="{if $attach[refcheck]}forum.php?mod=attachment{$is_archive}&aid=$aidencode{else}{$attach[url]}$attachthumb{/if}" inpost="1"{if $_GET['from'] != 'preview'} onmouseover="showMenu({'ctrlid':this.id,'pos':'12'})"{/if} />
<!--{else}-->
<img{if $attach['price'] && $_G['forum_attachmentdown'] && $_G['uid'] != $attach['uid']} class="attprice"{/if} id="aimg_$attach[aid]" aid="$attach[aid]" src="{STATICURL}image/common/none.gif" zoomfile="{if $attach[refcheck]}forum.php?mod=attachment{$is_archive}&aid=$aidencode&noupdate=yes¬humb=yes{else}{$attach[url]}$attach[attachment]{/if}" file="{if $attach[refcheck]}forum.php?mod=attachment{$is_archive}&aid=$aidencode&noupdate=yes{else}{$attach[url]}$attach[attachment]{/if}" $widthcode id="aimg_$attach[aid]" inpost="1"{if $_GET['from'] != 'preview'} onmouseover="showMenu({'ctrlid':this.id,'pos':'12'})"{/if} />
<!--{/if}-->
修改:
<!--{if $_G['setting']['thumbstatus'] && $attach['thumb']}-->
<img{if $attach['price'] && $_G['forum_attachmentdown'] && $_G['uid'] != $attach['uid']} class="attprice"{/if} style="cursor:pointer" id="aimg_$attach[aid]" aid="$attach[aid]" src="{STATICURL}image/common/none.gif" onclick="zoom(this, this.getAttribute('zoomfile'), 0, 0, '{$_G[setting][showexif]}')" zoomfile="{if $attach[refcheck]}forum.php?mod=attachment{$is_archive}&aid=$aidencode&noupdate=yes¬humb=yes{else}{$attach[url]}$attach[attachment]{/if}" file="{if $attach[refcheck]}forum.php?mod=attachment{$is_archive}&aid=$aidencode{else}{$attach[url]}$attachthumb{/if}"
alt="$_G[forum_thread][subject]- $_G[setting][sitename] " title="$_G[forum_thread][subject]- $_G[setting][sitename] " inpost="1"{if $_GET['from'] != 'preview'} onmouseover="showMenu({'ctrlid':this.id,'pos':'12'})"{/if} />
<!--{else}-->
<img{if $attach['price'] && $_G['forum_attachmentdown'] && $_G['uid'] != $attach['uid']} class="attprice"{/if} id="aimg_$attach[aid]" aid="$attach[aid]" src="{STATICURL}image/common/none.gif" zoomfile="{if $attach[refcheck]}forum.php?mod=attachment{$is_archive}&aid=$aidencode&noupdate=yes¬humb=yes{else}{$attach[url]}$attach[attachment]{/if}" file="{if $attach[refcheck]}forum.php?mod=attachment{$is_archive}&aid=$aidencode&noupdate=yes{else}{$attach[url]}$attach[attachment]{/if}" $widthcode alt="$_G[forum_thread][subject]- $_G[setting][sitename] " title="$_G[forum_thread][subject]- $_G[setting][sitename] " id="aimg_$attach[aid]" inpost="1"{if $_GET['from'] != 'preview'} onmouseover="showMenu({'ctrlid':this.id,'pos':'12'})"{/if} />
<!--{/if}-->
其实就是在代码里面添加了 alt="$_G[forum_thread][subject]- $_G[setting][sitename] " 这个代码而已。
全部改好之后直接替换回去。更新css缓存之类的。
如果不想修改代码的话直接替换小编改好的文件:
替换到 /template/default/forum/discuzcode.htm 或者你正在使用的模板下的对应途径
备注:此方法跟文件是基于Discuz!X3.2来修改,其他版本请自行测试,测试前请备份好文件。