Woocommerce:特色图片不同于产品图片 [英] Woocommerce: Featured image different than product image

查看:34
本文介绍了Woocommerce:特色图片不同于产品图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法为产品添加特色图片以显示在产品列表中,但显示另一张图片作为产品图片?(只是一张图片,而不是图片库)?

当点击列表中的产品(带有特色图片)时,该产品仅显示一张带有说明和内容的图片,但显示的是不同的图片,而不是特色图片.

解决方案

您可以将产品图库中的第一张图片设置为产品图片,您设置的特色图片将显示在产品列表页面上.

现在要将产品库中的第一张图片设置为产品图片,您需要覆盖 woocommerce 模板文件 - product-image.php.

要覆盖,请将文件从 woocommerce 插件文件夹复制到您的主题/woocommerce/single-product/product-image.php

部分替换为以下代码 -

<?php$attachment_ids = $product->get_gallery_attachment_ids();isset ($placeholder_width)?: $placeholder_width=0;isset ($placeholder_height)?: $placeholder_height=0;如果( $attachment_ids ){$attachment_id = $attachment_ids[0];如果(!$placeholder_width)$placeholder_width = $woocommerce->get_image_size('shop_catalog_image_width');如果(!$placeholder_height)$placeholder_height = $woocommerce->get_image_size('shop_catalog_image_height');$output = '

';//$classes = array( 'imagewrapper' );$classes = array();$image_link = wp_get_attachment_url( $attachment_id );如果($image_link){$image = wp_get_attachment_image( $attachment_id, apply_filters( 'single_product_thumbnail_size', 'shop_thumbnail' ) );$image_class = esc_attr( implode( ' ', $classes ) );$image_title = esc_attr( get_the_title( $attachment_id ) );echo apply_filters('woocommerce_single_product_image_html', sprintf('<a href="%s" itemprop="image" class="woocommerce-main-image zoom" title="%s" rel="prettyPhoto[product-gallery]">%s</a>', $image_link, $image_title, $image ), $post->ID );} 别的 {echo apply_filters('woocommerce_single_product_image_html', sprintf('<img src="%s" alt="Placeholder"/>', woocommerce_placeholder_img_src()), $post->ID );}}?><?php do_action('woocommerce_product_thumbnails');?>

同时覆盖 product-thumbnails.php 文件并在 foreach 循环之前添加以下代码.

unset($attachment_ids);

添加此行后,第一张图片将不会显示在产品图库中,只会显示为产品图片.

Is there a way to add a featured image for the product to show in the product listings, but display another image as the product image? (just an image, not gallery of images)?

When clicking the product in the listing (with a featured image) the product displays just an image, with the description and stuff, but a different image, not the featured.

解决方案

You can set the first image from Product Gallery as Product image and featured image that you set will be displayed on Product listing page.

Now to set your first image from Product Gallery as Product image, you need to override woocommerce template file - product-image.php.

To override, copy the file from woocommerce plugin folder to your theme/woocommerce/single-product/product-image.php

Replace the <div> part with the following code -

<div class="images">

<?php

    $attachment_ids = $product->get_gallery_attachment_ids();
    isset ($placeholder_width)? : $placeholder_width=0;
    isset ($placeholder_height)? : $placeholder_height=0;

    if ( $attachment_ids ) {
        $attachment_id = $attachment_ids[0];

    if ( ! $placeholder_width )
        $placeholder_width = $woocommerce->get_image_size( 'shop_catalog_image_width' );
    if ( ! $placeholder_height )
        $placeholder_height = $woocommerce->get_image_size( 'shop_catalog_image_height' );

        $output = '<div class="imagewrapper">';

        //$classes = array( 'imagewrapper' );
        $classes = array();
        $image_link = wp_get_attachment_url( $attachment_id );

        if ( $image_link ) {

        $image       = wp_get_attachment_image( $attachment_id, apply_filters( 'single_product_thumbnail_size', 'shop_thumbnail' ) );
        $image_class = esc_attr( implode( ' ', $classes ) );
        $image_title = esc_attr( get_the_title( $attachment_id ) );

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

        } else {

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

        }

    }
?>

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

Also override product-thumbnails.php file and add following code just before foreach loop.

unset($attachment_ids);

By adding this line, the first image will not be displayed in product gallery, only be displayed as Product image.

这篇关于Woocommerce:特色图片不同于产品图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆