【问题标题】:Remove featured image from showing on product display page从产品展示页面上删除特色图片
【发布时间】:2013-06-21 03:01:32
【问题描述】:

我正在使用 wooCommerce 和 Wordpress。 当您为 woocommerce 产品设置特色图片时,它会在产品详细信息页面的图库中显示为第一张/主图片。我不想要这个。我只是想完全从产品详细信息页面隐藏/删除特色图片。我希望特色图片仅显示在产品类别页面中,而不显示在单个产品详细信息页面上。 我尝试使用代码删除图像 remove_action( 'woocommerce_before_single_product_summary', 'woocommerce_show_product_images', 20 ); 在主题函数文件中。 仍然无法看到想要的结果。

我正在使用主题room09

我想隐藏此垂直图像(已将其设置为特色图像)仅在此页面上显示。

<div class="images">

<?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_url( get_post_thumbnail_id() );
        $attachment_count   = count( get_children( array( 'post_parent' => $post->ID, 'post_mime_type' => 'image', 'post_type' => 'attachment' ) ) );

        if ( $attachment_count != 1 ) {
            $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"  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" />', woocommerce_placeholder_img_src() ), $post->ID );

    }
?>

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

</div>

【问题讨论】:

  • 您是否购买了已安装 woocommerce 的主题?或者安装主题后作为插件添加?
  • 我在安装 woocommerce 后添加了主题......

标签: wordpress woocommerce


【解决方案1】:

要在不编辑任何主题文件的情况下执行此操作,只需将其添加到子主题中的插件或 functions.php 中:

add_filter('woocommerce_single_product_image_thumbnail_html', 'remove_featured_image', 10, 3);

function remove_featured_image($html, $attachment_id, $post_id) {
    $featured_image = get_post_thumbnail_id($post_id);
    if ($attachment_id != $featured_image) {
        return $html;
    }
    return '';
}

【讨论】:

  • 你能解释一下代码吗?这里的 10 和 3 指的是什么?
  • @SamarthAgarwal 10 和 3 由 add_filter 函数使用。 10 是调用它的顺序,因为您可以通过同一个过滤器拥有多个函数,而 3 是传递的变量数,因为默认值为 1。
【解决方案2】:

您需要编辑单品图片功能。不知道你有没有跟我一样的插件。

您可以在 woocommerce-hooks.php 的第 93 行为此阻止引用 add_action,它应该在插件文件夹的根目录中或在 yourtheme/woocommerce/...

//add_action( 'woocommerce_before_single_product_summary', 'woocommerce_show_product_images', 20 );
//add_action( 'woocommerce_product_thumbnails', 'woocommerce_show_product_thumbnails', 20 );

它应该删除它,如果它不这样做:

在 content-single-product.php 中引用或删除第 31 行:

//do_action( 'woocommerce_before_single_product_summary' );

编辑

不如在 CSS 中为特色图片添加 display:none。转到外观 - 编辑器并添加以下内容:

CSS:

.yith_magnifier_zoom_wrap{
    display: none !important;
}

【讨论】:

  • 嘿,我在 content-single-product.php 中评论了一行。但这删除了我所有的图像。我只需要删除特色图片。请检查页面。
  • 那么您到底想做什么......只删除大图像并保留产品的其他图像?
  • 我希望它显示在所有其他页面(小部件和类别页面等)上,但不在产品显示页面上。
  • 请在以后提出问题。添加 do_action 但在钩子文件中,将 'woocommerce_show_product_images' 与 'woocommerce_show_product_thumbnails' 交换。
  • 试试上面的方法,让我们看看你得到了什么
【解决方案3】:

终于解决了。对于遇到类似问题的人,将此代码添加到主题的函数文件中:

if ( ! empty( $attachment_ids ) ) array_unshift( $attachment_ids, get_post_thumbnail_id() );

if ( empty( $attachment_ids ) ) return;

这可能取决于您使用的主题。对代码进行必要的更改。

【讨论】:

  • 这仍然适用于当前版本的 WooCommerce 吗?对于目前正在努力解决这个问题的人有什么建议吗?谢谢。
【解决方案4】:

我在 functions.php 中添加了这两个 sn-ps,以从商店页面循环中删除徽章和特色图片。

// Remove product images from the shop loop
remove_action( 'woocommerce_before_shop_loop_item_title','woocommerce_template_loop_product_thumbnail', 10 );

// Remove sale badges from the shop loop
remove_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_show_product_loop_sale_flash', 10 );

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-09-13
    • 1970-01-01
    • 2017-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多