图片img标签自动添加alt,title属性

//图片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;
}

私密:图片A标签添加title属性

第二种

//文章图片自动添加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);

第3种

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' );
WP技巧

解决国内服务器wordpress无法更新问题

2018-1-4 12:14:25

WP技巧

All in One SEO Pack插件schema输出导致WP文章时间提前8小时bug修复

2022-9-9 14:29:59

个人中心
今日签到
有新私信 私信列表
搜索