百度站长平台那有个页面优化建议,每次 看的时候 都有个扣分的,ALT属性——方便图片收录,每次都提示我,添加ALT有助于收录,结果 上百篇文章了,让我怎么加?左司马?
最近发现一个神器代码,自动添加。。(其实也不是啦)
其实功能并不复杂,就是检测是否存在ALT属性,没有则按照文章名添加。
不过,看了下,只可以使img标签加的图片(呸,还有人用其他的标签加?span滚吧。。。)
function img_alt( $imgalt ){ global $post; $title = $post->post_title; $imgUrl = "<img\s[^>]*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');
放置到 主题下 functions.php就好了,不过只可以文章名,其实也不错啦!
899743 196841An fascinating discussion is price comment. I feel which you need to write extra on this topic, it may not be a taboo subject but usually individuals are not enough to speak on such topics. Towards the next. Cheers 288574
2018年8月14日 02:59506027 465771Oh my goodness! an remarkable write-up dude. Numerous thanks Even so My business is experiencing trouble with ur rss . Do not know why Struggle to sign up to it. Can there be everybody finding identical rss dilemma? Anyone who knows kindly respond. Thnkx 868789
2018年8月12日 15:52这个代码有点小瑕疵。使用后图片ALT信息前面会有 / 这个标识符,正常这个标注符应该在IMG标签的结尾,也就是 ALT=***** / >,而现在是 / ALT=**** >,求解决!谢谢!
2018年5月19日 21:46不错,学习了。
2016年8月18日 16:48