Wordpress/WooCommerce 删除默认图像尺寸?

dun*_*140 4 php wordpress woocommerce

是否可以从 WordPress 中删除默认图像?更具体地说,那些由 WooCommerce 创建的?我正在构建一个完全自定义的主题,为此我不需要任何 WC 图像大小,因此我不想将它们存储在我们的服务器上。我在下面的代码中尝试了 codex remove_image_size(),但这会破坏媒体库。

谢谢。

// Remove default WC image sizes
function wpdocs_remove_plugin_image_sizes() {
    remove_image_size( 'woocommerce_thumbnail' );
    remove_image_size( 'woocommerce_single' );
    remove_image_size( 'woocommerce_gallery_thumbnail' );
    remove_image_size( 'shop_catalog' );
    remove_image_size( 'shop_single' );
    remove_image_size( 'shop_thumbnail' );
}
add_action('init', 'wpdocs_remove_plugin_image_sizes');
Run Code Online (Sandbox Code Playgroud)

dun*_*140 6

经过进一步的调试,事实证明上述代码确实有效,但被同一文件中的另一段代码破坏了。

我在网上找不到太多引用此请求的信息,因此将答案留在这里供其他寻找相同内容的人使用。

放置在functions.php中

// Remove default WC image sizes
function remove_wc_image_sizes() {
    remove_image_size( 'woocommerce_thumbnail' );
    remove_image_size( 'woocommerce_single' );
    remove_image_size( 'woocommerce_gallery_thumbnail' );
    remove_image_size( 'shop_catalog' );
    remove_image_size( 'shop_single' );
    remove_image_size( 'shop_thumbnail' );
}
add_action('init', 'remove_wc_image_sizes');
Run Code Online (Sandbox Code Playgroud)

这也适用于 WordPress 中任何其他已注册的图像尺寸,您所需要做的就是找出要删除的图像尺寸的名称,并将其添加到上面的列表中。上面的列表当前删除了所有 WooCommerce 图像尺寸,以便您只剩下 Wordpress 默认尺寸以及您可能定义的任何其他自定义尺寸。

如果您不确定主题中注册的尺寸,请使用以下代码在管理中显示 array() 列表,以帮助您轻松识别。

放置在functions.php中

add_action( 'admin_init', 'theme_additional_images' );
// Display all image sizes other than the custom, default, thumbnail, medium and large

function theme_additional_images() {
    global $_wp_additional_image_sizes;
    $get_intermediate_image_sizes = get_intermediate_image_sizes();

    echo '<pre>' . print_r($_wp_additional_image_sizes) . '</pre>';
}
Run Code Online (Sandbox Code Playgroud)