【问题标题】:Remove featured image from the WooCommerce gallery从 WooCommerce 库中删除特色图片
【发布时间】:2017-11-27 16:59:49
【问题描述】:

我尝试使用其他帖子的建议,但它们不起作用。我希望不在我的产品图片area/image 画廊中显示特色产品图片,因为我将标题中的特色图片用作背景图片,并且它太宽而无法在画廊中。

到目前为止,这是最接近工作的方法,但出现错误。但是它确实可以满足我的需要。

有什么办法可以解决这个问题,这样我就不会收到错误消息了吗?

这是我的代码:

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 '';
}

这是错误:

/home/.../plugins/my-custom-functions/inc/php/functional.php(93) 中 remove_featured_image() 缺少参数 3:第 4 行的 eval() 代码

警告:/home…/plugins/my-custom-functions/inc/php/functional.php(93) 中 remove_featured_image() 缺少参数 3:第 4 行的 eval() 代码

【问题讨论】:

    标签: php wordpress image woocommerce product


    【解决方案1】:

    woocommerce_single_product_image_thumbnail_html 过滤器挂钩只有 2 个参数

    所以你必须稍微改变你的代码以避免错误,这样:

    add_filter('woocommerce_single_product_image_thumbnail_html', 'remove_featured_image', 10, 2);
    function remove_featured_image($html, $attachment_id ) {
        global $post, $product;
    
        $featured_image = get_post_thumbnail_id( $post->ID );
    
        if ( $attachment_id == $featured_image )
            $html = '';
    
        return $html;
    }
    

    代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件中。


    过滤器钩子woocommerce_single_product_image_thumbnail_html的参考:

    【讨论】:

    • 嗨@LoicTheAztec,这是一个有点旧的帖子 - 但你能协助编辑它和一系列产品而不是全局函数吗?我试过了,失败了。
    【解决方案2】:

    这就是我所做的,希望它有帮助。 我使用 Meta Box 插件在产品编辑页面(在 Wordpress 后端)中创建了一个自定义复选框。该复选框基本上是“从产品库中隐藏特色图片”,这是代码(放置在 functions.php 文件中):

    function hide_first_img( $meta_boxes ) {
    
    $prefix = 'meta_box';
    
    $meta_boxes[] = array(
        'id' => 'hide_img',
        'title' => esc_html__( 'Hide first image', 'custom_meta_boxes' ),
        'post_types' => array('product'),
        'context' => 'advanced',
        'priority' => 'default',
        'autosave' => 'false',
        'fields' => array(
            array(
                'id' => $prefix . '_hide_first_image',
                'name' => esc_html__( 'Hide first image in product gallery', 'custom_meta_boxes' ),
                'type' => 'checkbox',
                'desc' => esc_html__( 'Check to hide the first image in the gallery', 'custom_meta_boxes' ),
            ),
        ),
    );
    
    return $meta_boxes;
    }
    add_filter( 'rwmb_meta_boxes', 'hide_first_img' );
    

    然后我使用 LoicTheAztec 的代码,效果很好,仅在选中复选框时删除图像,如下所示(放置在 functions.php 文件中):

    function remove_featured_image($html, $attachment_id ) {
    
        if( rwmb_meta( 'meta_box_hide_first_image' )){
     
            global $post, $product;
    
            $featured_image = get_post_thumbnail_id( $post->ID );
    
            if ( $attachment_id == $featured_image )
                $html = '';
    
        }
    
        return $html;
    }
    add_filter('woocommerce_single_product_image_thumbnail_html', 'remove_featured_image', 10, 2);
    

    最后,仅针对 FLATSOME THEME 用户,为了同时隐藏图库中的第一个缩略图,我直接在我的子主题文件夹中编辑了以下文件:\flatsome-child\woocommerce\single-product\product-gallery-thumbnails。 php

    也许对于默认的 Woocomerce 画廊或其他主题有一些类似的文件:

    // show first thumbnail only if meta box is NOT checked
    <?php if( !rwmb_meta( 'meta_box_hide_first_image' )) : ?>
    
        <div class="col is-nav-selected first">
          <a>
            <?php
              $image_id = get_post_thumbnail_id($post->ID);
              $image =  wp_get_attachment_image_src( $image_id, apply_filters( 'woocommerce_gallery_thumbnail_size', 'woocommerce_'.$image_size ) );
            $image_alt = get_post_meta( $image_id, '_wp_attachment_image_alt', true );
              $image = '<img src="'.$image[0].'" alt="'.$image_alt.'" width="'.$gallery_thumbnail['width'].'" height="'.$gallery_thumbnail['height'].'" class="attachment-woocommerce_thumbnail" />';
    
              echo $image;
            ?>
          </a>
        </div>
    
      <?php endif; ?>
    

    我可以确认这正在我的网站上实时运行。

    【讨论】:

    • 嗨,Matteo,我也使用 Flatsome,我花了几天时间尝试在产品页面上更改它。 I can get the main product image to not show but then when a variation is selected, the variation image does not switch out where the main image is it switched out the first gallery image.
    猜你喜欢
    • 2021-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-13
    • 2011-07-05
    • 2018-02-22
    相关资源
    最近更新 更多