开发者

Creating a custom function to replace one in WordPress

开发者 https://www.devze.com 2023-01-08 06:36 出处:网络
I\'m using WordPress 3.0 and the \'the_post_thumbnail\' function to resize my images. The problem is that the function doesn\'t resize to exact dimensions when you don\'t specify a square image.

I'm using WordPress 3.0 and the 'the_post_thumbnail' function to resize my images. The problem is that the function doesn't resize to exact dimensions when you don't specify a square image.

// Works fine
add_image_size('my-image-size',100, 100, true);

// Image is only resized to width or height, not both
add_image_size('my-image-size',265, 开发者_如何学Go182, true);

I'm pretty sure this is a bug, as I feel the image should be cropped to both dimensions to make an exact size. I could just edit the 'image_resize_dimensions' function in media.php, but I'm wondering if there is a better way, some way to override that function with my own?

Thanks!

Darren.


Mark JaQuith has an article recommended by the Codex page on the_post_thumbnail function. It discusses using a different function "set_post_thumbnail_size() to change image sizes.

    set_post_thumbnail_size( 50, 50, true ); 
    // 50 pixels wide by 50 pixels tall, hard crop mode

It has a crop flag argument that you can pass to perform a hard crop to the exact dimensions you specify, or a soft crop, which works the way you are experiencing. I know the article discusses WP 2.9 specifically, but it might help.


In most of my theme development prior to 3.0 I used the timthumb plugin with much success. As i also found the built-in functions lacking. You can find the info here.


You can't override functions in PHP. You'll get a fatal error if you attempt to define a function with the same name within the same scope. Apparently you can use this: override_function() to override built-in functions. Not sure about ones define in other files though.

You could define another function (ie: add_image_size2) however and refactor the code to call that instead, but you'll end up modifying a lot more of the code than you would by just editing the function.

0

精彩评论

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