WooCommerce Singe产品自定义图库输出仅在第一张幻灯片上有效

时间:2019-03-19 作者:Darko

我将“product image.php”复制到我的主题文件夹中,以便稍微更改HTML输出,因此wc_get_gallery_image_html 我创建了我的函数并在那里更改了输出。

这是原件function

这是修改后的一个:

function my_custom_get_gallery_image_html( $attachment_id, $main_image = false ) {
$flexslider        = (bool) apply_filters( \'woocommerce_single_product_flexslider_enabled\', get_theme_support( \'wc-product-gallery-slider\' ) );
$gallery_thumbnail = wc_get_image_size( \'gallery_thumbnail\' );
$thumbnail_size    = apply_filters( \'woocommerce_gallery_thumbnail_size\', array( $gallery_thumbnail[\'width\'], $gallery_thumbnail[\'height\'] ) );
$image_size        = apply_filters( \'woocommerce_gallery_image_size\', $flexslider || $main_image ? \'woocommerce_single\' : $thumbnail_size );
$full_size         = apply_filters( \'woocommerce_gallery_full_size\', apply_filters( \'woocommerce_product_thumbnails_large_size\', \'full\' ) );
$thumbnail_src     = wp_get_attachment_image_src( $attachment_id, $thumbnail_size );
$full_src          = wp_get_attachment_image_src( $attachment_id, $full_size );
$image             = wp_get_attachment_image(
    $attachment_id,
    $image_size,
    false,
    apply_filters(
        \'woocommerce_gallery_image_html_attachment_image_params\',
            array(
                \'title\'                   => get_post_field( \'post_title\', $attachment_id ),
                \'data-caption\'            => get_post_field( \'post_excerpt\', $attachment_id ),
                \'data-src\'                => $full_src[0],
                \'data-large_image\'        => $full_src[0],
                \'data-large_image_width\'  => $full_src[1],
                \'data-large_image_height\' => $full_src[2],
                \'class\'                   => $main_image ? \'wp-post-image\' : \'\',
            ),
        $attachment_id,
        $image_size,
        $main_image
    )
);

return \'<div style="background-image:url(\' . esc_url( $full_src[0] ) . \');" data-thumb="\' . esc_url( $thumbnail_src[0] ) . \'" class="woocommerce-product-gallery__image"><a href="\' . esc_url( $full_src[0] ) . \'"></a></div>\';
}
我尝试使用背景图像而不是滑块内的图像,但它仅在第一张幻灯片上加载样式。

顺便说一句,我添加了此部分style="background-image:url(\' . esc_url( $full_src[0] ) . \');"

1 个回复
最合适的回答,由SO网友:Darko 整理而成

显然,为了使其正常工作,还有第二个文件(product thumbnails.php)使用相同的函数,因此如果在一个文件中更改了默认函数,那么第二个文件也必须更新。