【问题标题】:Woocommerce: Featured image different than product image revision neededWoocommerce:特色图像与需要的产品图像修订不同
【发布时间】:2016-01-06 04:23:44
【问题描述】:

我一直在尝试实现页面上讨论的修改:Woocommerce: Featured image different than product image

但它似乎不适用于 woocommerce v2.4.7 的最新版本 有人对如何解决它有任何想法吗?

这是我试图替换的 v2.4.7 的当前代码。当应用修复时,它会破坏页面。

<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">

<?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' ); ?>

</div>

任何帮助将不胜感激。

谢谢 达雷尔

【问题讨论】:

    标签: wordpress image woocommerce image-gallery featured


    【解决方案1】:

    Hege 用 product-image.php 页面的替换代码做对了

    要仅删除第一个缩略图,而不是全部,请使用

    unset($attachment_ids[0]);
    

    关于product-thumbnails.php

    【讨论】:

      【解决方案2】:

      把这个:

      unset($attachment_ids); // Don't display first image in product gallery, only as product image
      

      就在 product-thumbnails.php 中的 foreach 循环之前。对我来说效果很好。

      【讨论】:

        【解决方案3】:

        我在这里一直在苦苦挣扎,但这似乎对我有用:

        <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 )
                $size = wc_get_image_size( 'shop_catalog' );
                $placeholder_width = $size['width'];
            if ( ! $placeholder_height )
                $size = wc_get_image_size( 'shop_catalog' );
                $placeholder_width = $size['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_large_thumbnail_size', 'shop_single' ) );
                $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" 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' ); ?>
        

        最近才弄明白,所以它可能需要一些收尾工作,但可能会为您指明正确的方向。

        但是,我无法从图库中删除产品图片。应该实现这一点的代码是这样的(添加到 product-thumbnails.php 中):

        unset($attachment_ids);
        

        这对我来说就是删除所有画廊缩略图。也许你已经弄清楚了这一点?

        【讨论】:

          猜你喜欢
          • 2014-08-29
          • 1970-01-01
          • 2020-11-22
          • 1970-01-01
          • 1970-01-01
          • 2021-11-10
          • 1970-01-01
          • 2020-01-26
          • 1970-01-01
          相关资源
          最近更新 更多