我有很多图片必须通过媒体库上传。我想知道,是否可以将文件名添加到alt
上载属性字段时是否自动执行?目前,我必须打开每个图像并添加alt
手动设置属性。
将图像上载到媒体库时自动添加Alt属性
2 个回复
SO网友:Christine Cooper
可行的解决方案Ifound 正在将帖子标题添加到缺少ALT
属性
将以下内容添加到functions.php
文件:
function add_alt_tags($content)
{
global $post;
preg_match_all(\'/<img (.*?)\\/>/\', $content, $images);
if(!is_null($images))
{
foreach($images[1] as $index => $value)
{
if(!preg_match(\'/alt=/\', $value))
{
$new_img = str_replace(\'<img\', \'<img alt="\'.$post->post_title.\'"\', $images[0][$index]);
$content = str_replace($images[0][$index], $new_img, $content);
}
}
}
return $content;
}
add_filter(\'the_content\', \'add_alt_tags\', 99999);
或者,如果要用文件名替换属性,可以使用get_attached_file 的函数$new_img
变量SO网友:Arun Basil Lal
我知道我参加聚会已经很晚了,但我不能放过这一次,因为我为此编写了一个插件:Auto Image Attributes From Filename With Bulk Updater
顾名思义,还有一个批量更新程序,因此您也可以更新现有图像。
结束