【绝对数值】纯代码实现WordPress图片自动添加alt属性和title属性

为什么要做图片自动添加alt属性和title属性?因为真的效果非常好 网站已经一年了都没收录过图片,自从做了这个SEO 以后 没多久 我就发现了图片被收录了 哈哈哈 

为什么要做自动的?你手动一张张来 要累死呀  直接用代码让系统自动为你生成图片的属性关联~

[rihide]参考https://www.22vd.com/44010.html 引用http://www.xianje.cn/forum.php?mod=viewthread&tid=408&extra=

把下面的 绝对数值代码 放到 主题的 functions.php 

/**
    *自动添加图片 alt 和 title 属性
*/
function image_alttitle( $imgalttitle ){
        global $post;
        $category = get_the_category();
        $flname=$category[0]->cat_name;
        $btitle = get_bloginfo();
        $imgtitle = $post->post_title;
        $imgUrl = "<img\s[^>]*src=(\"??)([^\" >]*?)\\1[^>]*>";
        if(preg_match_all("/$imgUrl/siU",$imgalttitle,$matches,PREG_SET_ORDER)){
                if( !empty($matches) ){
                        for ($i=0; $i < count($matches); $i++){
                                $tag = $url = $matches[$i][0];
                                $j=$i+1;
                                $judge = '/title=/';
                                preg_match($judge,$tag,$match,PREG_OFFSET_CAPTURE);
                                if( count($match) < 1 )
                                $altURL = ' alt="'.$imgtitle.' '.$flname.' 第'.$j.'张" title="'.$imgtitle.' '.$flname.' 第'.$j.'张-'.$btitle.'" ';
                                $url = rtrim($url,'>');
                                $url .= $altURL.'>';
                                $imgalttitle = str_replace($tag,$url,$imgalttitle);
                        }
                }
        }
        return $imgalttitle;
}
add_filter( 'the_content','image_alttitle');

[/rihide]   

图片[1]-【绝对数值】纯代码实现WordPress图片自动添加alt属性和title属性-自购网

怎么检测有没有做好呢?

图片[2]-【绝对数值】纯代码实现WordPress图片自动添加alt属性和title属性-自购网

THE END