这就是在客户端的主题中获取其他img属性的方法
// Get images attributes
$images = array();
foreach ( $galleries as $meta_key => $gallery ) {
$attachment_ids = array_filter( explode( ',', $gallery ) );
foreach ( $attachment_ids as $key => $attachment_id ) {
$full_size_image = wp_get_attachment_image_src( $attachment_id, 'full' );
$single = wp_get_attachment_image_src( $attachment_id, 'shop_single' );
$thumbnail = wp_get_attachment_image_src( $attachment_id, 'shop_thumbnail' );
$catalog = wp_get_attachment_image_src( $attachment_id, 'shop_catalog' );
$images[ $meta_key ][ $key ] = array(
'single' => $single[0],
'single_w' => $single[1],
'single_h' => $single[2],
'thumbnail' => $thumbnail[0],
'catalog' => $catalog[0],
'data-src' => $full_size_image[0],
'data-large_image' => $full_size_image[0],
'data-large_image_width' => $full_size_image[1],
'data-large_image_height' => $full_size_image[2],
);
}
}
我需要以完全相同的方式获取和输出alt属性,如何做到?
output_thumb += '<div>';
output_thumb += '<img width="' + image['single_w'] + '" height="' + image['single_h'] + '" src="' + img_single + '" class="attachment-shop_single size-shop_single" data-src="' + image['data-src'] + '" data-large_image="' + image['data-large_image'] + '" data-large_image_width="' + image['data-large_image_width'] + '" data-large_image_height="' + image['data-large_image_height'] + '"/>';
output_thumb += '</div>';
它必须是类似的东西
output_thumb += '<div>';
output_thumb += '<img alt="' + image['alt'] + '" width="' + image['single_w'] + '" height="' + image['single_h'] + '" src="' + img_single + '" class="attachment-shop_single size-shop_single" data-src="' + image['data-src'] + '" data-large_image="' + image['data-large_image'] + '" data-large_image_width="' + image['data-large_image_width'] + '" data-large_image_height="' + image['data-large_image_height'] + '"/>';
output_thumb += '</div>';
类似的东西
var img_alt = $(this).attr('alt');
output_thumb += '<img alt="img_alt" .....
返回alt=";img_ alt";
我找到的解决方案:
// Get default gallery
$product = new WC_product( $product_id );
$attachment_ids = $product->get_gallery_image_ids();
if ( has_post_thumbnail( $product_id ) ) {
$attachment_ids = $result = array_merge( array( get_post_thumbnail_id( $product_id ) ), $attachment_ids );
}
$usedImages = array();
foreach ( $attachment_ids as $key => $attachment_id ) {
$full_size_image = wp_get_attachment_image_src( $attachment_id, 'full' );
$single = wp_get_attachment_image_src( $attachment_id, 'shop_single' );
$thumbnail = wp_get_attachment_image_src( $attachment_id, 'shop_thumbnail' );
$catalog = wp_get_attachment_image_src( $attachment_id, 'shop_catalog' );
$image_alt = get_post_meta( $attachment_id, '_wp_attachment_image_alt', true);
if (!in_array($single[0], $usedImages)) {
$usedImages[] = $single[0];
$images['default_gallery'][ $key ] = array(
'single' => $single[0],
'single_w' => $single[1],
'single_h' => $single[2],
'thumbnail' => $thumbnail[0],
'catalog' => $catalog[0],
'data-src' => $full_size_image[0],
'data-large_image' => $full_size_image[0],
'data-large_image_width' => $full_size_image[1],
'data-large_image_height' => $full_size_image[2],
'alt' => $image_alt,
);
}
}
return $images;
}
output_thumb += '<div>';
output_thumb += '<img alt="' + image['alt'] + '" width="' + image['single_w'] + '" height="' + image['single_h'] + '" src="' + img_single + '" class="attachment-shop_single size-shop_single" data-src="' + image['data-src'] + '" data-large_image="' + image['data-large_image'] + '" data-large_image_width="' + image['data-large_image_width'] + '" data-large_image_height="' + image['data-large_image_height'] + '"/>';
output_thumb += '</div>';