Wordpress图像alt文本

时间:2014-09-25 17:16:39

标签: php wordpress

我打算在此页http://www.venaproducts.com/dev/product/fosmon-hybo-duoc-case-for-amazon-fire-phone/

上打印出每个缩略图的替代文字

然而,即使替代文字不同,第一张图片替代文字似乎也用于所有缩略图。非常感谢任何帮助。

global $post, $product, $woocommerce;

$attachment_ids = $product->get_gallery_attachment_ids();

if ( $attachment_ids ) {
?>
<div id="product-images" class="thumbnails"><?php
    $small_img   = wp_get_attachment_image_src(get_post_thumbnail_id(),'single-product-thumbnail');
    $middle_img  = wp_get_attachment_image_src(get_post_thumbnail_id(),'single-product');
    $large_img   = wp_get_attachment_image_src(get_post_thumbnail_id(),'single-product-zoom');
    $img_id = get_post_thumbnail_id(get_the_id());
    $alt_text = get_post_meta($img_id, '_wp_attachment_image_alt', true);


    echo '<a href="#" data-image="' . $middle_img[0] . '" data-zoom-image="' . $large_img[0] . '">
              <img src="' . $small_img[0] . '" alt="' . $alt_text . '">
          </a>';
    $loop = 0;
    $columns = apply_filters( 'woocommerce_product_thumbnails_columns', 3 );

    foreach ( $attachment_ids as $attachment_id ) {

        $classes = array( 'zoom' );

        if ( $loop == 0 || $loop % $columns == 0 )
            $classes[] = 'first';

        if ( ( $loop + 1 ) % $columns == 0 )
            $classes[] = 'last';

        $image_link = wp_get_attachment_url( $attachment_id );

        if ( ! $image_link )
            continue;

        // $image       = wp_get_attachment_image( $attachment_id, apply_filters( 'single_product_small_thumbnail_size', 'shop_thumbnail' ) );
        // $image_class = esc_attr( implode( ' ', $classes ) );
        // $image_title = esc_attr( get_the_title( $attachment_id ) );
        $small_img = wp_get_attachment_image_src( $attachment_id, 'single-product-thumbnail' );
        $middle_img = wp_get_attachment_image_src( $attachment_id, 'single-product' );
        $large_img = wp_get_attachment_image_src( $attachment_id, 'single-product-zoom' );

        // echo apply_filters( 'woocommerce_single_product_image_thumbnail_html', sprintf( '<a href="%s" class="%s" title="%s"  rel="prettyPhoto[product-gallery]">%s</a>', $image_link, $image_class, $image_title, $image ), $attachment_id, $post->ID, $image_class );
        echo '<a href="#" data-image="' . $middle_img[0] . '" data-zoom-image="' . $large_img[0] . '">
                  <img src="' . $small_img[0] . '" alt="' . $alt_text . '">
              </a>';

        $loop++;
    }

?></div>
<div class="carousel-prev product-prev"></div>
<div class="carousel-next product-next"></div>
<?php

}

1 个答案:

答案 0 :(得分:1)

在foreach循环之外处理后缩略图时设置$alt_text值。

在你的foreach循环中,当你浏览附件时,你忘了更新值。

在foreach循环中尝试这样的事情:

$small_img = wp_get_attachment_image_src( $attachment_id, 'single-product-thumbnail' );
$middle_img = wp_get_attachment_image_src( $attachment_id, 'single-product' );
$large_img = wp_get_attachment_image_src( $attachment_id, 'single-product-zoom' );
$alt_text = get_post_meta($attachment_id, '_wp_attachment_image_alt', true);