Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Wordpress img alt text_Php_Wordpress - Fatal编程技术网

Php Wordpress img alt text

Php Wordpress img alt text,php,wordpress,Php,Wordpress,我希望打印出这个页面上每个缩略图的alt文本 但是,第一个图像alt文本似乎用于所有缩略图,即使alt文本不同。非常感谢您的帮助 global $post, $product, $woocommerce; $attachment_ids = $product->get_gallery_attachment_ids(); if ( $attachment_ids ) { ?> <div id="product-images" class="thumbnails"><

我希望打印出这个页面上每个缩略图的alt文本

但是,第一个图像alt文本似乎用于所有缩略图,即使alt文本不同。非常感谢您的帮助

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
global$post、$product、$woocmerce;
$attachment_id=$product->get_gallery_attachment_id();
如果($attachment_id){
?>

在foreach循环之外处理post缩略图时,可以设置
$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);

谢谢Thomas Misund,但是你能详细解释一下你的意思吗?我还是有点迷茫。你是说我需要把$alt_文本放在for循环语句中吗?就是这样。谢谢Thomas Misund