腾轩网 - 专注优质QQ活动-绿色软件-游戏辅助-技术教程分享网!

kqq

靓号
优惠券极风
流量
流量

WordPress程序实现自动给文章图片添加alt和titleb标签信息方法教程

腾轩网 其他教程
扫码手机访问WordPress程序实现自动给文章图片添加alt和titleb标签信息方法教程
0

教程介绍

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

三种方法

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

第一种方法:

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

第二种方法:

/** 自动给图片添加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.tengxuanw.com */

第三种方法:

/** 图自动加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.tengxuanw.com */

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

标签:

暂无标签
    协助本站seo优化一下,谢谢!
    关键词不能为空

免责声明:

本站提供的资源,都来自网络,版权争议与本站无关,所有内容及软件的文章仅限用于学习和研究目的。不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负,我们不保证内容的长久可用性,通过使用本站内容随之而来的风险与本站无关,您必须在下载后的24个小时之内,从您的电脑/手机中彻底删除上述内容。如果您喜欢该程序,请支持正版软件,购买注册,得到更好的正版服务。侵删请致信E-mail: 571533527@qq.com

同类推荐
分享
发布评论

微信公众号

热门文章
随机推荐
未分类 WordPress程序实现自动给文章图片添加alt和titleb标签信息方法教程
教程介绍 现在百度越来越注重图片的优化效果,那么网站优化不可避免的需要给图片添加ALT属性? 根据百度百科,ALT属性用于HTML,输入纯笔墨参数属性...
扫描二维码阅读原文
腾轩网 January, 01
初始化 ×