wordpress图片img标签自动添加alt,title属性(纯代码)

小初seo 网站建设评论1,086字数 1319阅读4分23秒阅读模式

文章目录[隐藏]

第一种

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

第二种

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

 最后更新:2021-4-27
  • 本文由 小初seo 发表于 2021年4月25日10:53:33
  • 转载请务必保留本文链接:https://www.pkak.cn/web/200.html
匿名

发表评论

匿名网友
:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

拖动滑块以完成验证