programing

WooCommerce 갤러리에서 피쳐 이미지 삭제

linuxpc 2023. 3. 12. 10:31
반응형

WooCommerce 갤러리에서 피쳐 이미지 삭제

다른 게시물의 제안을 사용해보았지만 효과가 없습니다.제품 이미지에 특장 제품 이미지를 표시하지 않으려고 합니다.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()'d 코드

경고:/home…/plugins/my-custom-functions/inc/php/functional.php(93)에 remove_featured_image()에 대한 인수 3이 없습니다. 4행의 코드입니다.

필터 후크 인수는 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;
}

코드가 기능합니다.php 파일 또는 임의의 플러그인 파일에 있는 활성 자식 테마(또는 테마)입니다.


필터 후크에 대한 참조:

  • woocommerce 템플릿:
  • woocommerce 템플릿:

이게 내가 한 일이니까 도움이 되길 바라.Meta Box 플러그인을 사용하여 제품 편집 페이지(Wordpress 백엔드)에 커스텀 체크박스를 만들었습니다.체크박스는 기본적으로 "제품 갤러리에서 피처 이미지 숨기기"이며, 다음은 (기능에 배치되는) 코드입니다.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' );

그리고 Loic The Aztec의 코드를 사용하여 이렇게 체크박스를 켜야만 이미지를 삭제할 수 있습니다.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 TEME 사용자만 갤러리의 첫 번째 섬네일을 숨기기 위해 자녀테마 폴더에 있는 파일을 직접 편집했습니다: \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; ?>

지금 제 사이트에서 라이브로 동작하고 있는 것을 확인할 수 있습니다.

언급URL : https://stackoverflow.com/questions/44732859/remove-featured-image-from-the-woocommerce-gallery

반응형