- This topic has 5 replies, 2 voices, and was last updated 3 years, 4 months ago by
David.
-
AuthorPosts
-
November 25, 2022 at 2:00 am #2432529
Sam
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?
November 25, 2022 at 4:22 am #2432772David
StaffCustomer SupportHi 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 Largesize 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/
November 25, 2022 at 7:23 am #2433109Sam
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.
November 25, 2022 at 7:44 am #2433154David
StaffCustomer SupportNo, you keep your snippet. And ignore mine. Your’s should now list your new image sizes in the editor select box.
The
textdomainsimply tells WP where to look for any possible translations for that string.
For example ours isgeneratepressand 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.
November 25, 2022 at 8:00 am #2433376Sam
Will do that, Thank you.
November 25, 2022 at 8:25 am #2433424David
StaffCustomer SupportYou’re welcome
-
AuthorPosts
- You must be logged in to reply to this topic.