A-A+

自动为wordpress文章图片添加alt属性和title属性

这个技巧,相信大家也都是很期盼的,会省心省力很多!

其实很简单了,看一下就能看懂吧:

 

/**
*自动为wordpress文章图片添加alt属性和title属性
*/
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.'张-'.$btitle.'" title="'.$imgtitle.' '.$flname.' 第'.$j.'张-'.$btitle.'" ';
$url = rtrim($url.'');
$url .=  $altURL.'';
$imgalttitle = str_replace($tag,$url,$imgalttitle);
}
}
}
return $imgalttitle;
}
add_filter( 'the_content','image_alttitle');

 

 

 

 

 

 

给我留言(如果文章当中有隐藏内容,请评论之后刷新当前页面。请认真评论,否则一律不给予通过!!!

icon_question icon_razz icon_sad icon_evil icon_exclaim icon_smile icon_redface icon_biggrin icon_surprised icon_eek icon_confused icon_cool icon_lol icon_mad icon_twisted icon_rolleyes icon_wink icon_idea icon_arrow icon_neutral icon_cry icon_mrgreen