第一种
//图片img标签添加alt,title属性
function imagesalt($content) {
global $post;
$pattern = "//i";
$replacement = '';
$content = preg_replace($pattern, $replacement, $content);
return $content;
}
add_filter('the_content', 'imagesalt');
//图片A标签添加title属性
function aimagesalt($content) {
global $post;
$pattern = "//i";
$replacement = '';
$content = preg_replace($pattern, $replacement, $content);
return $content;
}
add_filter('the_content', 'aimagesalt');
/* 高亮显示搜索关键词
/* ------------------- */
function search_word_replace($buffer){
if(is_search()){
$arr=explode(' ',get_search_query());
foreach($arr as $v){
if($v)$buffer=preg_replace('/('.$v.')/i',"$1",$buffer);
}
}
return $buffer;
}
第二种
//文章图片自动添加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);
第三种
function image_alt($c) {
global $post;//全局变量
$title = $post->post_title;//文章标题
$s = array('/src="(.+?.(jpg|bmp|png|jepg|gif))"/i' => 'src="$1" alt="'.$title.'"');
foreach($s as $p => $r){
$c = preg_replace($p,$r,$c);
}
return $c;
}
add_filter( 'the_content', 'image_alt' );
评论