How do you set only the image width of a Wordpress

2019-04-28 18:12发布

I am trying to set the width of a post thumbnail using

the_post_thumbnail().

The width of the image needs to be 210px but the height is not meant to be fixed as all the images will be different sizes. I have tried

the_post_thumbnail(array(210,0))

but this does not work. Any ideas?

Thanks

标签: wordpress
4条回答
兄弟一词,经得起流年.
2楼-- · 2019-04-28 18:54

Open your themes functions.php file and add your image size:

Example:

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

Breakdown of the code:

  • 'slider'= The name of the new image size (use this when adding the new image size to your template file).
  • 525 = The image width
  • 339 = The image height
  • true = The image will be cropped to the exact dimensions listed.

Usage: Use a current template (index.php, single.php, etc.), or create your own. Place this inside the loop (usually just after the_title();) :

the_post_thumbnail('slider');

Now when you add a page or post that uses the template you added the code above, use the featured image option to upload an image. It will output to the size you've created.

Note: This will only apply to newly uploaded images.

查看更多
Rolldiameter
3楼-- · 2019-04-28 19:00

Did you try this : Thumbnail Custom Size

It is the best way to change Thumbnail Size.

查看更多
迷人小祖宗
4楼-- · 2019-04-28 19:03

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

查看更多
家丑人穷心不美
5楼-- · 2019-04-28 19:13

Add this inside 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 your theme's template files

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

For 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   
}

Reference: Here.

查看更多
登录 后发表回答