如何仅设置 Wordpress 缩略图的图像宽度 [英] How do you set only the image width of a Wordpress thumbnail

查看:32
本文介绍了如何仅设置 Wordpress 缩略图的图像宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用

the_post_thumbnail().

the_post_thumbnail().

图片的宽度需要为 210 像素,但高度并不一定是固定的,因为所有图片的尺寸都不同.我试过了

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))

the_post_thumbnail(array(210,0))

但这不起作用.有什么想法吗?

but this does not work. Any ideas?

谢谢

推荐答案

在你的functions.php中添加这个

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' ); } ?>

对于默认缩略图,将此添加到您的functions.php

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   
}

参考: 此处.

这篇关于如何仅设置 Wordpress 缩略图的图像宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆