【问题标题】:Use different size thumbnails for subcategories and products in Woocommerce在 Woocommerce 中为子类别和产品使用不同大小的缩略图
【发布时间】:2018-02-23 09:41:51
【问题描述】:

我需要为子类别和产品设置不同的拇指大小,这可能吗?

我需要显示一个带有 500 x 100 缩略图的子类别列表和一个带有 300 x 390 缩略图的产品列表。

我已经在 woocommerce> 设置 > 产品 > 显示 > 产品图片中进行了设置,但我无法为子类别或产品设置不同的尺寸。

【问题讨论】:

  • 请也发布您的代码
  • 我什么都没有编码,不是 woocommerce 的选项吗?
  • 关于你实际所做的事情的更多细节可能有助于获得帮助......

标签: wordpress woocommerce


【解决方案1】:

哈哈,我开发了一个干净的解决方案:

function register_size_image() {
   add_image_size( 'category_thumb', 1170, 585,true );
   add_image_size( 'product_thumb', 750, 940,true );
}

add_action( 'after_setup_theme', 'register_size_image' );


function size_of_category_thumb($u)
{
    return array(1170, 585,true);
}
add_filter('subcategory_archive_thumbnail_size', 'size_of_category_thumb');

function size_of_product_thumb($u)
{
    return array(750, 940,true);
}
add_filter('single_product_archive_thumbnail_size', 'size_of_product_thumb');

【讨论】:

    【解决方案2】:

    是的,这很有可能。请按照以下步骤操作 -

    1. 添加新的图像尺寸。为此,将以下代码添加到主题的 functions.php

      add_image_size('你的自定义尺寸', 500, 100); // 您可以通过使用不同的值重复此行来添加多个图像大小(根据您的需要)

    2. 在这一步之后上传图片。 如果再次上传图片开销太大,可以使用this plugin

      这将创建所有指定尺寸的缩略图。

    3. 现在在您的主题中,无论您在哪里显示具有以下功能的图像 -

      the_post_thumbnail( 'your-custom-size' ) 或 wp_get_attachment_image( 42, 'your-custom-size' ),将您的图像尺寸名称作为参数传递。

    【讨论】:

      【解决方案3】:

      我解决了覆盖 woocommerce 的功能,我把它放在我的 functions.php 上

      add_image_size( 'category_thumb', 500, 100,1 );
      add_image_size( 'product_thumb', 300, 390,1 );
      
      function woocommerce_subcategory_thumbnail( $category ) {
          $small_thumbnail_size       = 'category_thumb';
          $thumbnail_id           = get_woocommerce_term_meta( $category->term_id, 'thumbnail_id', true );
      
          if ( $thumbnail_id ) {
                  $image        = wp_get_attachment_image_src( $thumbnail_id, $small_thumbnail_size );
                  $image        = $image[0];
                  $image_srcset = function_exists( 'wp_get_attachment_image_srcset' ) ? wp_get_attachment_image_srcset( $thumbnail_id, $small_thumbnail_size ) : false;
                  $image_sizes  = function_exists( 'wp_get_attachment_image_sizes' ) ? wp_get_attachment_image_sizes( $thumbnail_id, $small_thumbnail_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 );
      
                  // Add responsive image markup if available
                  if ( $image_srcset && $image_sizes ) {
                          echo '<img src="' . esc_url( $image ) . '" alt="' . esc_attr( $category->name ) . '" width="' . esc_attr( $dimensions['width'] ) . '" height="' . esc_attr( $dimensions['height'] ) . '" srcset="' . esc_attr( $image_srcset ) . '" sizes="' . esc_attr( $image_sizes ) . '" />';
                  } else {
                          echo '<img src="' . esc_url( $image ) . '" alt="' . esc_attr( $category->name ) . '" width="' . esc_attr( $dimensions['width'] ) . '" height="' . esc_attr( $dimensions['height'] ) . '" />';
                  }
          }
      }
      
      function woocommerce_get_product_thumbnail( $size = 'shop_catalog', $deprecated1 = 0, $deprecated2 = 0 ) 
      {
            global $product;
      //        $image_size = apply_filters( 'single_product_archive_thumbnail_size', $size );
          return $product ? $product->get_image( 'product_thumb' ) : '';
         }
      

      有效!

      【讨论】:

        【解决方案4】:
        function yourFunctionName()
        {
            return array(1140, 300,true);
        }
        add_filter('subcategory_archive_thumbnail_size', 'yourFunctionName');
        

        为我工作

        【讨论】:

          猜你喜欢
          • 2021-02-08
          • 2014-12-05
          • 1970-01-01
          • 1970-01-01
          • 2021-05-24
          • 2023-03-22
          • 1970-01-01
          • 2021-06-24
          • 2019-02-09
          相关资源
          最近更新 更多