欢迎光临
我们一直在努力

WordPress程序实现自动给文章图片添加alt和title信息利于网站SEO方法教程

现在百度越来越注重图片的优化效果,那么网站优化不可避免的需要给图片添加ALT属性? 根据百度百科,ALT属性用于HTML,输入纯笔墨参数属性。 次要影响是对于其他图片的无助识别,一个补救的优化步骤。 ALT 属性通过添加 ALT 标签来表示。 ALT属性是唯一能识别图片信息就显得极其紧张了。 每当我们浏览一张图片时,将鼠标移到图片上,呈现的笔墨就是ALT属性,不仅可以识别搜索引擎,还可以给用户很好的体验。

首先将下面的代码复制粘贴到/inc/functions.php文件里面

第一种方法:

/** 自动给图片添加Alt标签www.qyun.vip */
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.'>';
                                $img<a href="https://www.qyun.vip/tag/alt" title="【查看含有[alt]标签的文章】" target="_blank">alt</a><a href="https://www.qyun.vip/tag/title" title="【查看含有[title]标签的文章】" target="_blank">title</a> = str_replace($tag,$url,$img<a href="https://www.qyun.vip/tag/alt" title="【查看含有[alt]标签的文章】" target="_blank">alt</a>title);
                        }
                }
        }
        return $imgalttitle;
}
add_filter( 'the_content','image_alttitle');
/** 自动给图片添加Alt标签www.qyun.vip */

第二种方法:

/** 自动给图片添加Alt标签 */
function image_alt_tag($content){
global $post;preg_match_all('/<img (.*?)\/>/', $content, $images);
if(!is_null($images)) {foreach($images[1] as $index => $value)
{
$new_img = str_replace('<img', '<img alt="'.get_the_title().'-'.get_bloginfo('name').'"', $images[0][$index]);
$content = str_replace($images[0][$index], $new_img, $content);}}
return $content;
}
add_filter('the_content', 'image_alt_tag', 99999);
/** 自动给图片添加Alt标签www.qyun.vip */

第三种方法:

/** 图自动加alt和title标签属性 */
function image_alt_tag($content){
    global $post;preg_match_all('/<img (.*?)\/>/', $content, $images);
    if(!is_null($images)) {foreach($images[1] as $index => $value)
    {$new_img = str_replace('<img', '<img alt="'.get_the_title().'-'.get_bloginfo('name').'" title="'.get_the_title().'-'.get_bloginfo('name').'"', $images[0][$index]);
    $content = str_replace($images[0][$index], $new_img, $content);}}
    return $content;
}
add_filter('the_content', 'image_alt_tag', 99999);
/** 自动给图片添加alt和title标签www.qyun.vip */

三种代码都可行,自己选择适合那种,都是不会影响你正常的给图片添加“替代文本”的操作。

赞(0)
未经允许不得转载:桔子博客 » WordPress程序实现自动给文章图片添加alt和title信息利于网站SEO方法教程
分享到: 更多 (0)

相关推荐

  • 暂无文章

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址