Wordpress Woocommerce alt图像中的文字

时间:2015-11-29 16:18:06

标签: php wordpress woocommerce

我正在构建一个Woocommerce,但我无法在前端显示Alt Text。

我正在处理的代码是:

<?php
    if ( has_post_thumbnail() ) {

        $image_title    = esc_attr( get_the_title( get_post_thumbnail_id() ) );
        $image_caption  = get_post( get_post_thumbnail_id() )->post_excerpt;
        $image_alt      = esc_attr( get_post_meta($attachment_id, '_wp_attachment_image_alt', true) );
        $image_link     = wp_get_attachment_url( get_post_thumbnail_id() );
        $image          = get_the_post_thumbnail( $post->ID, apply_filters( 'single_product_large_thumbnail_size', 'shop_single' ), array(
            'title' => $image_title,
            'alt'   => $image_alt
            ) );

        $attachment_count = count( $product->get_gallery_attachment_ids() );

        if ( $attachment_count > 0 ) {
            $gallery = '[product-gallery]';
        } else {
            $gallery = '';
        }

        echo apply_filters( 'woocommerce_single_product_image_html', sprintf( '<a href="%s" itemprop="image" class="woocommerce-main-image zoom" title="%s" data-rel="prettyPhoto' . $gallery . '">%s</a>', $image_link, $image_caption, $image ), $post->ID );

    } else {

        echo apply_filters( 'woocommerce_single_product_image_html', sprintf( '<img src="%s" alt="' . $image_alt . '" />', wc_placeholder_img_src(), __( 'Placeholder', 'woocommerce' ) ), $post->ID );

    }
?>

<?php do_action( 'woocommerce_product_thumbnails' ); ?>

这种随地吐痰的代码是:

<a href="https://openwallsg..." itemprop="image" class="woocommerce-main-image zoom" title="" rel="lightbox">
   <img width="800" height="800" src="https://openwal..." class="attachment-shop_single wp-post-image" alt="" title="bandages bad food">
</a>

我不明白为什么alt仍然是空的?

1 个答案:

答案 0 :(得分:0)

在wp_get_attachment_image之前有一个_(下划线),删除它以获得“alt”值......

参考:https://codex.wordpress.org/Function_Reference/wp_get_attachment_image