Wocommerce全尺寸img与大 - product-image.php

时间:2017-02-05 16:29:48

标签: php wordpress woocommerce

我想用woocommerce lighbox上的大图像替换全尺寸图像。

这是我的代码 -

    <?php
   if ( has_post_thumbnail() ) {
        $image              = get_the_post_thumbnail( $post->ID, apply_filters( 'single_product_large_thumbnail_size', 'shop_single' ) );
        $image_title        = esc_attr( get_the_title( get_post_thumbnail_id() ) );
        $image_link         = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID) );
        $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="bigbox woocommerce-main-image zoom" title="%s" rel="prettyPhoto' . $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" class="bigbox" />', woocommerce_placeholder_img_src() ), $post->ID );

    }
?>

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

我试图将$ image_link更改为

$image_link = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'large' ); 

没有运气。

2 个答案:

答案 0 :(得分:1)

$ image_link以数组形式出现。回显图像链接,试试,

<?php echo $image_link[0]; ?> 因此,您的完整代码应该像this

答案 1 :(得分:1)

WooCommerce有过滤器&#39; woocommerce_single_product_image_html&#39; ,您不会为此重复主题中的代码。

您可以在functions.php或函数插件中添加以下内容。

注意:这适用于2.6x并且不适用于2.7,因为他们更改了单个图像显示。

function yourprefix_woocommerce_single_product_image_html( $html, $post_id ) {

    if ( ! class_exists( 'WooCommerce' ) ) return; 
    //bail if WooCommerce is not installed and active since we are using the WC_VERSION constant

    if ( version_compare( WC_VERSION, '2.7', '<' ) ) {

        $image_title = esc_attr( get_the_title( get_post_thumbnail_id() ) );
        $image_link = wp_get_attachment_image_src( get_post_thumbnail_id(), 'large' ); //this is where and use the [0] on this variable in the sprintf    
        $image = get_the_post_thumbnail( $post_id, 'shop_single', array( 'title' => $image_title ) );
        $html = sprintf('<a href="%s" itemprop="image" class="woocommerce-main-image zoom" title="%s">%s</a>', $image_link[0], $image_title, $image  );

    } //end version compare still return the html so we don't get an error

    return $html;

}
add_filter( 'woocommerce_single_product_image_html', 'yourprefix_woocommerce_single_product_image_html', 10, 2 );

注意:wp_get_attachment_image_src()不是默认代码中使用的,它是wp_get_attachment_image_url()获取上传的图像,而不是大小的图像。如果你想生成的图像使用$ variable = wp_get_attachment_image_src(...);然后是$变量[0]为url,$ variable [1] width,$ variable [2] height。这有很多关键,所以我不会重复