开发者

Crop thumbnail to exact dimensions on Wordpress for medium size

开发者 https://www.devze.com 2023-01-11 05:01 出处:网络
In Wordpress Media Settings the Thumbnail size we can crop开发者_JAVA百科 thumbnail to exact dimensions.

In Wordpress Media Settings the Thumbnail size we can crop开发者_JAVA百科 thumbnail to exact dimensions.

But for the Medium size no option to get exact dimensions. There have a way to force exact dimensions for medium size?

If yes, how to implement to the functions.php


If you want medium images to be cropped instead of scaled, like you can choose for the thumbnail size in the options page (but not for medium or large sizes), you can set the medium_crop option to true in your functions.php:

if(false === get_option("medium_crop")) {
    add_option("medium_crop", "1");
} else {
    update_option("medium_crop", "1");
}


As of now, there's a better way to do it. You can overwrite the default image sizes—namely thumbnail, medium and large—using the add_image_size function.

Examples:

// Set "Thumbnail" image size
if( function_exists( 'add_theme_support' ) ) {

    add_theme_support( 'post-thumbnails' );
    set_post_thumbnail_size( 150, 150, false );

}

// Set "Medium" and "Large" image sizes
if( function_exists( 'add_image_size' ) ) {

    add_image_size( 'medium', 300, 9999, false );
    add_image_size( 'large', 1024, 9999, false );

}


Add this to your functions.php:

add_action( 'init', create_function( '', 'add_image_size( "cropped_medium", 300, 150, true );' ) );

The arguments are size name, width, height, and whether to force a crop.

0

精彩评论

暂无评论...
验证码 换一张
取 消