WordPress纯代码给图片添加alt和title属性
在很多站点上图片上都添加了alt属性
文章中给图片添加alt和title属性,针对图片SEO优化
下面就是使用代码给wordpress文章图片添加该属性(站长亲测有用)
方法一:
//给文章图片添加alt,title属性 function img_alt( $imgalt ){ global $post; $title = $post->post_title; $imgUrl = "]*src=("??)([^" >]*?)1[^>]*>"; if(preg_match_all("/$imgUrl/siU",$imgalt,$matches,PREG_SET_ORDER)){ if( !empty($matches) ){ for ($i=0; $i < count($matches); $i++){ $tag = $url = $matches[$i][0]; $judge = '/alt=/'; preg_match($judge,$tag,$match,PREG_OFFSET_CAPTURE); if( count($match) < 1 ) $altURL = ' alt="'.$title.'" '; $url = rtrim($url,'>'); $url .= $altURL.'>'; $imgalt = str_replace($tag,$url,$imgalt); } } } return $imgalt; } add_filter( 'the_content','img_alt');
方法二:
//给文章图片添加alt,title属性 function image_alt_tag($content){ global $post;preg_match_all('//', $content, $images); if(!is_null($images)) {foreach($images[1] as $index => $value) {$new_img = str_replace(' 复制上面任何一个代码,加入到主题的 functions.php 文件中,然后刷新网站右键检查图片,就会看到已经添加了alt属性
版权声明:
作者:Mankey
链接:https://l0h.cn/390.html
来源:零零猴资源库
文章版权归作者所有,未经允许请勿转载。
THE END
0
二维码
海报
WordPress纯代码给图片添加alt和title属性
在很多站点上图片上都添加了alt属性
文章中给图片添加alt和title属性,针对图片SEO优化
下面就是使用代码给wordpress文章图片添加该属性(站长亲测有用)
方法……

共有 0 条评论