13071077710
您的当前位置: 首页 > wordpress > 给文章图片自动添加alt和title信息

给文章图片自动添加alt和title信息

时间: 2016-11-4

//给文章图片自动添加alt和title信息
add_filter('the_content', 'imagesalt');
function imagesalt($content) {
 global $post;
 $pattern ="/<a(.*?)href=('|\")(.*?).(bmp|gif|jpeg|jpg|png)('|\")(.*?)>/i";
 $replacement = '<a$1href=$2$3.$4$5 alt="'.$post->post_title.'" title="'.$post->post_title.'"$6>';
 $content = preg_replace($pattern, $replacement, $content);
 return $content;
}
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().'"', $images[0][$index]);
 $content = str_replace($images[0][$index], $new_img, $content);}}
 return $content;
}
add_filter('the_content', 'image_alt_tag', 99999);

Copyright © 2013-2019 子歇    版权所有   网站地图