Archived topic
Content-Template Image Size
5 replies · Started by Sam on November 25, 2022
Hello,
I'm using a Content-Template element for my blog post (single page).
At the top of the post, I'm pulling a dynamic image from a Custom Field using the Image Block's dynamic data.
Everything works as expected except for the image size,
my image size is 768px for standard screen or 1536px for retina, so by loading the "Large" size 1024px or 2048px, which is the closest image size available for me (see attached image), I'm losing load time and bandwidth.
Can a different, more appropriate size be used for that image?
Hi there,
if you add this PHP Snippet to your site:
add_filter( 'image_size_names_choose', function() {
return [
'thumbnail' => __( 'Thumbnail', 'textdomain' ),
'medium' => __( 'Medium', 'textdomain' ),
'medium_large' => __( 'Medium Large', 'textdomain' ),
'large' => __( 'Large', 'textdomain' ),
'full' => __( 'Full Size', 'textdomain' ),
];
} );
WP creates a Medium Large size of 768px. And that snippet will allow it to be selected in the block editor.
How to add PHP: https://docs.generatepress.com/article/adding-php/
Hello David,
This is the code I added while waiting for your reply
add_action( 'init', function() {
add_image_size( 'my-archive-featured-image', 768, 500, true ); // 500 width, 200 height, crop
add_image_size( 'my-single-featured-image', 1536, 1000 ); // for retina
} );
add_filter( 'image_size_names_choose', 'my_custom_sizes' );
function my_custom_sizes( $sizes ) {
return array_merge( $sizes, array(
'my-archive-featured-image' => __( 'my-archive-featured-image' ),
) );
}
Should I replace it with the snippet above?
also what's 'textdomain' in thumbnail' => __( 'Thumbnail', 'textdomain' )
Thank you.
No, you keep your snippet. And ignore mine. Your's should now list your new image sizes in the editor select box.
The textdomain simply tells WP where to look for any possible translations for that string.
For example ours is generatepress and when a user chooses a different language for the thene it will check the themes .po language files to see if there is a translation fro that string.
If you don't intend to add any translation support then you don't require it.
Will do that, Thank you.
You're welcome