How to set WordPress thumbnail image width only

I am trying to set the width of the message thumbnail using

the_post_thumbnail ().

The image width should be 210 pixels, but the height should not be fixed, since all images will be of different sizes. I tried

the_post_thumbnail (array (210.0))

but it does not work. Any ideas?

thanks

+6
source share
4 answers

Add this to your functions. php

if ( function_exists( 'add_image_size' ) ) { add_image_size( 'category-thumb', 210, 9999 ); //210 pixels wide (and unlimited height) } 

Use it inside theme template files

 <?php if ( has_post_thumbnail() ) { the_post_thumbnail( 'category-thumb' ); } ?> 

For the default thumbnail, add this inside your functions. php

 if ( function_exists( 'add_theme_support' ) ) { add_theme_support( 'post-thumbnails' ); set_post_thumbnail_size( 150, 9999 ); // default Post Thumbnail dimensions } 

Link: Here .

+19
source

Change the_post_thumbnail(array(210,0)) to the_post_thumbnail(array(210,9999))

+2
source

Open your functions.php files and add the image size:

Example:

 add_image_size('slider', 525, 339, true); 

Code Breakdown:

  • 'slider' = Name of the new image size (use this when adding a new image size to the template file).
  • 525 = Image Width
  • 339 = Image Height
  • true = The image will be cropped to fit the exact size.

Application: Use the current template (index.php, single.php, etc.) Or create your own. Put this inside a loop (usually right after the_title(); ) :

 the_post_thumbnail('slider'); 

Now, when you add a page or message that uses a template, you have added the above code, use the advanced image function to load the image. It will be displayed on the size that you created.

Note: This only applies to recently uploaded images.

0
source

Have you tried this: Custom thumbnail size

This is the best way to resize thumbnails.

-1
source

Source: https://habr.com/ru/post/910916/


All Articles