返回介绍

set_post_thumbnail_size()

发布于 2017-09-11 10:22:05 字数 3177 浏览 913 评论 0 收藏 0

set_post_thumbnail_size( int $width,  int $height,  bool|array $crop = false )

Registers an image size for the post thumbnail.


description


参数

$width

(int) (Required) Image width in pixels.

$height

(int) (Required) Image height in pixels.

$crop

(bool|array) (Optional) Whether to crop images to specified width and height or resize. An array can specify positioning of the crop area.

Default value: false


源代码

File: wp-includes/media.php

function set_post_thumbnail_size( $width = 0, $height = 0, $crop = false ) {
	add_image_size( 'post-thumbnail', $width, $height, $crop );
}

更新日志

Versiondescription
2.9.0Introduced.

相关函数

Uses

  • wp-includes/media.php: add_image_size()

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 1You must log in to vote on the helpfulness of this note Contributed by Codex

    Default Usage
    To be used in the current theme’s functions.php file.

    
    function wpdocs_setup_theme() {
    	add_theme_support( 'post-thumbnails' );
    	set_post_thumbnail_size( 150, 150 );
    }
    add_action( 'after_setup_theme', 'wpdocs_setup_theme' );
    
  2. Crop Mode
    Set the default Post Thumbnail size by resizing the image proportionally (that is, without distorting it):

    
    set_post_thumbnail_size( 50, 50 ); // 50 pixels wide by 50 pixels tall, resize mode
    

    Set the default Post Thumbnail size by cropping the image (either from the sides, or from the top and bottom):

    
    set_post_thumbnail_size( 50, 50, true ); // 50 pixels wide by 50 pixels tall, crop mode
    

    Set the default Post Thumbnail size by cropping the image from top left:

    
    set_post_thumbnail_size( 50, 50, array( 'top', 'left')  ); // 50 pixels wide by 50 pixels tall, crop from the top left corner
    

    Set the default Post Thumbnail size by cropping the image from the center:

    
    set_post_thumbnail_size( 50, 50, array( 'center', 'center')  ); // 50 pixels wide by 50 pixels tall, crop from the center
    

    Note:
    This function will not resize your existing featured images. To regenerate existing images in the new size, use the Regenerate Thumbnails plugin.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文