WordPress代码实现自动给图片添加alt和title

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

图片[1]_WordPress代码实现自动给图片添加alt和title_黑猫博客

教程 请将下面的代码复制粘贴到/inc/functions/functions.php文件里面

第一种:

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

第二种:

/** 图自动加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.lt2.cc */

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

© 版权声明
THE END
点赞8打赏 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容