• Hi,

    I am having trouble setting a custom size for the WooCommerce thumbnail images using custom code. I need to do it with PHP instead of using the product settings in the backend.

    It isn’t cropping the images like the built in WooCommerce product settings does. Any image uploaded should be set at 400px wide by 645px tall. Currently the following code only resizes the image to 400px wide, so you’ll have images that are shorter than 645px high:

    <?php
    $args = array(
    ‘number’ => $number,
    ‘orderby’ => $orderby,
    ‘order’ => $order,
    ‘hide_empty’ => $hide_empty,
    ‘include’ => ‘14,15,16,20’,
    );

    $product_categories = get_terms( ‘product_cat’, $args );

    $count = count($product_categories);
    if ( $count > 0 ){
    echo “

      “;
      foreach ( $product_categories as $product_category ) {
      $thumbnail_id = get_woocommerce_term_meta( $product_category->term_id, ‘thumbnail_id’, true );
      $image = wp_get_attachment_image( $thumbnail_id, ‘mainimages’ );
      $alt = get_post_meta($thumbnail_id, ‘_wp_attachment_image_alt’, true);
      $category_count++;
      echo ‘<li class=”category’.$category_count.’ ‘. $product_category->slug.'”>

      <p><span></span></p>
      ‘.$image.’

      ‘;

      }
      echo “

    “;
    }
    ?>

    Custom size set in the functions file:

    if ( function_exists( ‘add_image_size’ ) ) {
    add_image_size( ‘mainimages’, 400, 645, true );
    }

    Thanks in advanced,

    Mark

  • The topic ‘WordPress/WooCommerce Sizes’ is closed to new replies.