缩略图筛选器不适用于自定义分类术语

时间:2022-02-09 作者:challenge2022

我正在使用挂钩更改网站上类别的外观。

我的问题是,它适用于产品类别,但当我决定使用自定义分类法时,它们出现了,但没有它们的图像(我用ACF设置了一个图像字段用于自定义税;返回格式图像数组),缺少的是自定义税项图像,而其他一切都有效(显示了自定义税链接和名称)

我不知道哪里出了问题,我尝试了无数不同的东西,它找不到自定义分类法的缩略图ID。

谁能给我一些建议吗。

if ( ! function_exists(\'custom_subcategory_thumbnail\') ):

        remove_action(\'woocommerce_before_subcategory_title\', \'woocommerce_subcategory_thumbnail\');

        add_action(\'woocommerce_before_subcategory_title\', \'custom_subcategory_thumbnail\', 10, 1);

        function custom_subcategory_thumbnail( $category ) {

    

            $thumbnail_id           = get_term_meta( $category->term_id, \'thumbnail_id\', true );
            
        /*    echo \'<pre>\';
            var_dump($testing);
            echo \'</pre>\';*/
        //    var_dump($thumbnail_id);

            
          //  $taxonomy = $queried_object->taxonomy;
           // $wpimage = get_field(\'workplace_thumb\', \'workplaces\' . \'_\' . $category->term_id);
          /*  echo \'<pre>\';
            print_r($thumbnail_id);echo \'</pre>\';*/
           
         //   $attachment_id = get_field( \'workplace_thumb\', \'workplaces_\' . $category->term_id );
    
/*
            echo \'<pre>\';
            print_r($attachment_id);
            echo \'</pre>\';*/
            if ( $thumbnail_id ) {

                $image        = wp_get_attachment_image_src( $thumbnail_id, \'woocommerce_single\' );

                $image        = $image[0];

                $image_srcset = function_exists( \'wp_get_attachment_image_srcset\' ) ? wp_get_attachment_image_srcset( $thumbnail_id, \'shop_single_image_size\' ) : false;

                $image_sizes  = function_exists( \'wp_get_attachment_image_sizes\' ) ? wp_get_attachment_image_sizes( $thumbnail_id, \'shop_single_image_size\' ) : false;

            }
            
            
            else{

                $image        = wc_placeholder_img_src();

                $image_srcset = $image_sizes = false;

            }

    

            if ( $image ) {

                // Prevent esc_url from breaking spaces in urls for image embeds

                // Ref: https://core.trac.wordpress.org/ticket/23605

                $image = str_replace( \' \', \'%20\', $image );

    

                echo \'<div class="banner-box__image brand-grayscale">
                <a href="\' . esc_url( get_term_link( $category, \'product_cat\' ) ) . \'" >
                <span class=" woocommerce-loop-category__thumbnail brand-grayscale" style="background-image :url(\' . esc_url( $image ) . \');"></span></a>

            </div>\';

            }

        }

        endif;

1 个回复
SO网友:challenge2022

我想出来了,万一有人需要,

它非常简单,只是在找不到id时使用了elseif&;将该分类id的自定义字段名称更改为自定义分类图像字段(workplace\\u thumb)。

  if ( ! function_exists(\'custom_subcategory_thumbnail\') ):

        remove_action(\'woocommerce_before_subcategory_title\', \'woocommerce_subcategory_thumbnail\');

        add_action(\'woocommerce_before_subcategory_title\', \'custom_subcategory_thumbnail\', 10, 1);

        function custom_subcategory_thumbnail( $category ) {

    

            $thumbnail_id           = get_term_meta( $category->term_id, \'thumbnail_id\', true );
           /* $term = get_field(\'workplace_thumb\', \'workplace_\' . $category->post_ID);


            echo \'<pre>\';
            echo "thumbnail id variable value vardump: ";echo \'<br />\';
            var_dump($thumbnail_id); echo \'<br />\';
            echo \'term variable value: \';echo \'<br />\';
           var_dump($term);echo \'<br />\';
           echo "Category value:";echo \'<br />\';
           var_dump($category);echo \'<br />\';
           echo \'</pre>\';*/
            
          //  $taxonomy = $queried_object->taxonomy;
           // $wpimage = get_field(\'workplace_thumb\', \'workplaces\' . \'_\' . $category->term_id);
          /*  echo \'<pre>\';
            print_r($thumbnail_id);echo \'</pre>\';*/
           
         //   $attachment_id = get_field( \'workplace_thumb\', \'workplaces_\' . $category->term_id );
    
/*
            echo \'<pre>\';
            print_r($attachment_id);
            echo \'</pre>\';*/
            if ( $thumbnail_id ) {

                $image        = wp_get_attachment_image_src( $thumbnail_id, \'woocommerce_single\' );

                $image        = $image[0];

                $image_srcset = function_exists( \'wp_get_attachment_image_srcset\' ) ? wp_get_attachment_image_srcset( $thumbnail_id, \'shop_single_image_size\' ) : false;

                $image_sizes  = function_exists( \'wp_get_attachment_image_sizes\' ) ? wp_get_attachment_image_sizes( $thumbnail_id, \'shop_single_image_size\' ) : false;

            }elseif ( ! $thumbnail_id ){
                $thumbnail_id_workplace             = get_term_meta( $category->term_id, \'workplace_thumb\', true );
                $image        = wp_get_attachment_image_src( $thumbnail_id_workplace, \'woocommerce_single\' );

                $image        = $image[0];

                $image_srcset = function_exists( \'wp_get_attachment_image_srcset\' ) ? wp_get_attachment_image_srcset( $thumbnail_id_workplace, \'shop_single_image_size\' ) : false;

                $image_sizes  = function_exists( \'wp_get_attachment_image_sizes\' ) ? wp_get_attachment_image_sizes( $thumbnail_id_workplace, \'shop_single_image_size\' ) : false;
            }
          
            
            else{

                $image        = wc_placeholder_img_src();

                $image_srcset = $image_sizes = false;

            }

    

            if ( $image ) {

                // Prevent esc_url from breaking spaces in urls for image embeds

                // Ref: https://core.trac.wordpress.org/ticket/23605

                $image = str_replace( \' \', \'%20\', $image );

    

                echo \'<div class="banner-box__image brand-grayscale">
                <a href="\' . esc_url( get_term_link( $category, \'product_cat\' ) ) . \'" >
                <span class=" woocommerce-loop-category__thumbnail brand-grayscale" style="background-image :url(\' . esc_url( $image ) . \');"></span></a>

            </div>\';

            }

        }

        endif;

相关推荐