当前位置:首页 > SEOWordPress > 正文

『SEO』WordPress文章图片自动添加ALT属性

百度站长平台那有个页面优化建议,每次 看的时候 都有个扣分的,ALT属性——方便图片收录,每次都提示我,添加ALT有助于收录,结果 上百篇文章了,让我怎么加?左司马?

『SEO』WordPress文章图片自动添加ALT属性 — 幻杀博客

最近发现一个神器代码,自动添加。。(其实也不是啦)

其实功能并不复杂,就是检测是否存在ALT属性,没有则按照文章名添加。

不过,看了下,只可以使img标签加的图片(呸,还有人用其他的标签加?span滚吧。。。)

function img_alt( $imgalt ){
        global $post;
        $title = $post->post_title;
        $imgUrl = "<img\s[^>]*src=(\"??)([^\" >]*?)\\1[^>]*>";
        if(preg_match_all("/$imgUrl/siU",$imgalt,$matches,PREG_SET_ORDER)){
                if( !empty($matches) ){
                        for ($i=0; $i < count($matches); $i++){
                                $tag = $url = $matches[$i][0];
                                $judge = '/alt=/';
                                preg_match($judge,$tag,$match,PREG_OFFSET_CAPTURE);
                                if( count($match) < 1 )
                                $altURL = ' alt="'.$title.'" ';
                                $url = rtrim($url,'>');
                                $url .= $altURL.'>';
                                $imgalt = str_replace($tag,$url,$imgalt);
                        }
                }
        }
        return $imgalt;
}

add_filter( 'the_content','img_alt');

放置到 主题下 functions.php就好了,不过只可以文章名,其实也不错啦!

标签:
上一篇: 下一篇:

23 条评论

评论加载中...
  1. 14楼
    Serbia Opera Mini Mac OS X
    PK Studies  

    899743 196841An fascinating discussion is price comment. I feel which you need to write extra on this topic, it may not be a taboo subject but usually individuals are not enough to speak on such topics. Towards the next. Cheers 288574

    2018年8月14日 02:59 评论
  2. 506027 465771Oh my goodness! an remarkable write-up dude. Numerous thanks Even so My business is experiencing trouble with ur rss . Do not know why Struggle to sign up to it. Can there be everybody finding identical rss dilemma? Anyone who knows kindly respond. Thnkx 868789

    2018年8月12日 15:52 评论
  3. 12楼
    来自天朝的朋友 谷歌浏览器 Windows 7
    Yemeitu  

    这个代码有点小瑕疵。使用后图片ALT信息前面会有 / 这个标识符,正常这个标注符应该在IMG标签的结尾,也就是 ALT=***** / >,而现在是 / ALT=**** >,求解决!谢谢!

    2018年5月19日 21:46 评论
  4. 11楼
    来自天朝的朋友 谷歌浏览器 Ubuntu Linux
    Dazi  

    不错,学习了。

    2016年8月18日 16:48 评论

发表评论

不理你。不要啊!吃饭。吃惊。吃西瓜。飞吻!恭喜!Hi纠结!膜拜!OK抛媚眼。泡泡糖。抛钱。忍!生闷气!调皮。偷看。委屈。献花。疑问?抓狂!

小提示:Ctrl+Enter快速提交助您一臂之力~
加载中……