Resize post thumbnail
-
22-04-2021 - |
Question
I'm trying to ensure that all assets are the same size, no matter what resolution they are uploaded in. I'm stuck finding outdated info on the matter, I think.
I've gotten to the following snippet
if( get_the_post_thumbnail_url()){
$image = wp_get_image_editor(get_the_post_thumbnail_url());
if ( !is_wp_error( $image )){
$image->resize( 300, 300, true );
$image->save("image.jpg");
}
}
Which seems to work, but I have no idea how I get the resized URL (or insert it properly). As I want to add it to my page. Something like
echo '<img src=" . $image.url() . ">";
Is what I'm thinking of. Am I going about this wrong, or how should I continue from here?
Thanks!
La solution
Why don't you use this add_image_size( 'custom-thumbnail', 300, 300, true );
in funtions.php
and call it <img src="<?php echo get_the_post_thumbnail_url('custom-thumbnail'); ?>">
in your page?
Autres conseils
Maybe providing ID may help.
if( get_the_post_thumbnail_url()){
$image = wp_get_image_editor(get_the_post_thumbnail_url(get_the_ID()));
if ( !is_wp_error( $image )){
$image->resize( 300, 300, true );
$image->save("image.jpg");
}
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à wordpress.stackexchange