首页 文章

从Woocommerce中删除单个产品页面中的特色图像

提问于
浏览
0

我有一个带有Woocommerce的Wordpress安装 . 商店的每件商品都有一个 Featured Image ,它将在 Shop Page and Single Product Page 中显示,而 Gallery Images 只会在 Single Product Page 中显示(首先是拇指,在点击灯箱后更大) .

这是由以下代码生成的:

<div class="images">

    <?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_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_title
                ) );

            $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="%s" />', wc_placeholder_img_src(), __( 'Placeholder', 'woocommerce' ) ), $post->ID );

        }
    ?>

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

</div>

这在正常的单一产品页面中将产生以下内容:

<div class="images">

    <a href="br1-opression.jpg" 
       itemprop="image" 
       class="woocommerce-main-image zoom" 
       title="" 
       data-rel="prettyPhoto[product-gallery]">

            <img width="750" 
                 height="544" 
                 src="br1-opression.jpg" 
                 class="attachment-shop_single wp-post-image" 
                 alt="br1-opression" 
                 title="br1-opression">
    </a>

    <div class="thumbnails columns-3">

        <a href="br1-opression-1.jpg" 
           class="zoom first" 
           title="br1-opression-1" 
           data-rel="prettyPhoto[product-gallery]">

                <img width="180" 
                     height="180" 
                     src="br1-opression-1-180x180.jpg" 
                     class="attachment-shop_thumbnail" 
                     alt="br1-opression-1">
        </a>

        <a href="br1-opression-2.jpg" 
           class="zoom" 
           title="br1-opression-2" 
           data-rel="prettyPhoto[product-gallery]">

            <img width="180" 
                 height="120" 
                 src="br1-opression-2-180x120.jpg" 
                 class="attachment-shop_thumbnail" 
                 alt="br1-opression-2">
        </a>

    </div>

</div>

正如您所看到的,它基本上将精选图像(大尺寸)和图库图像(在特色图像下更小)放在一个图库中 .

我想要实现的目标如下:

  • 从单个产品页面删除特色图片

  • 从图库中拍摄第一张图片,放入大图(与特色图片大小相同)

  • 将其他拇指放在它下面

长话短说,我想在单品页面中 replace the Featured image with the First image from the gallery .

到目前为止,我已经尝试过:

使用Functions.php中的CSS和新钩子隐藏特色图像 .

它确实会杀死特色图片,但它仍会显示在灯箱中 . 并且拇指仍然很小,因此单个产品页面中没有大图像,这是不可取的 .

有什么想法或想法吗?

1 回答

  • 1

    你可以试试这个:

    $imgid = $product->get_gallery_attachment_ids();
    

    get_gallery_attachment_ids() 函数获取数组中的所有图库ID,因此现在您可以通过以下方式获取该数组的第一个图像:

    <a href="<?php echo wp_get_attachment_url( $imgid[0] ); ?>" class="woocommerce-main-image zoom"><img src="<?php echo wp_get_attachment_url( $imgid[0] ); ?>" alt=""></a>
    

相关问题