答案 0 :(得分:2)
这会将商品标题添加到商店页面上的alt标签。
function modify_shop_product_image ( $img, $product, $size, $attr, $placeholder ) {
$alt_tag = 'alt=';
$pos = stripos( $img, 'alt=' ) + strlen( $alt_tag ) + 1;
return substr_replace($img, $product->get_name(), $pos, 0);
}
add_action( 'woocommerce_product_get_image', 'modify_shop_product_image', 10, 5 );
答案 1 :(得分:0)
这是正确的代码段
add_filter('wp_get_attachment_image_attributes', 'change_attachement_image_attributes', 20, 2);
function change_attachement_image_attributes( $attr, $attachment ){
// Get post parent
$parent = get_post_field( 'post_parent', $attachment);
// Get post type to check if it's product
$type = get_post_field( 'post_type', $parent);
if( $type != 'product' ){
return $attr;
}
if ( isset( $attr['class'] ) && 'custom-logo' === $attr['class'] ) {
return $attr;
}
/// Get title
$title = get_post_field( 'post_title', $parent);
$attr['alt'] = $title;
$attr['title'] = $title;
return $attr;
}
答案 2 :(得分:0)
添加到function.php
add_filter('wp_get_attachment_image_attributes', 'change_attachement_image_attributes', 20, 2);
function change_attachement_image_attributes($attr, $attachment) {
global $post;
if ($post->post_type == 'product') {
$title = $post->post_title;
$attr['alt'] = $title;
$attr['title'] = $title;
}
return $attr;
}