【发布时间】:2021-07-30 11:59:33
【问题描述】:
我在 functions.php 中使用此代码将“alt”文本设置为等于“title”,因为缺少 alt 文本。
“标题”等于包含连字符的图像文件名。
这一直有效,直到我尝试使用 str_replace('-', ' ', $string); 从“title”和随后的“alt”中删除连字符。
我是一个完整的编码新手,这就是为什么我的尝试不仅没有奏效,而且可能还有很长的路要走。
这有可能实现吗?
add_filter('wp_get_attachment_image_attributes', 'change_attachement_image_attributes', 20, 2);
function change_attachement_image_attributes($attr, $attachment) {
global $post;
$product = wc_get_product( $post->ID );
if ($post->post_type == 'product') {
$title = $post->post_title;
$replace = str_replace("-", " ", $title);
$attr['alt'] = $attr['replace'];
}
return $attr;
}
【问题讨论】:
标签: wordpress woocommerce alt