image_size_names_choose
云策文档标注
概述
image_size_names_choose 是一个 WordPress 过滤器,用于修改默认图像尺寸的名称和标签,使自定义图像尺寸在 WordPress 媒体库中可供管理员选择。
关键要点
- 过滤器参数为 $size_names,是一个数组,键为图像尺寸名称,值为标签,默认包括 'Thumbnail'、'Medium'、'Large' 和 'Full Size'。
- 主要用途是让自定义图像尺寸通过 add_image_size() 添加后,能在媒体库中显示为可选选项。
- 过滤器在 WordPress 3.3.0 版本中引入,常用于主题或插件开发中。
代码示例
// 添加自定义图像尺寸
function custom_theme_setup() {
add_image_size( 'custom-size-1', 1200, 300, true );
add_image_size( 'custom-size-2', 1000, 333, true );
}
add_action( 'after_setup_theme', 'custom_theme_setup' );
// 使自定义尺寸在媒体库中可选
function custom_image_sizes( $size_names ) {
$new_sizes = array(
'custom-size-1' => __( 'Custom Size #1', 'generatewp.com' ),
'custom-size-2' => __( 'Custom Size #2', 'generatewp.com' ),
);
return array_merge( $size_names, $new_sizes );
}
add_filter( 'image_size_names_choose', 'custom_image_sizes' );
原文内容
Filters the names and labels of the default image sizes.
Parameters
$size_namesstring[]-
Array of image size labels keyed by their name. Default values include
'Thumbnail','Medium','Large', and ‘Full Size’.
Source
$size_names = apply_filters(
'image_size_names_choose',
array(
'thumbnail' => __( 'Thumbnail' ),
'medium' => __( 'Medium' ),
'large' => __( 'Large' ),
'full' => __( 'Full Size' ),
)
);
Changelog
| Version | Description |
|---|---|
| 3.3.0 | Introduced. |
Skip to note 3 content
Rami Yushuvaev
Add a new image sizes to the Media Library .
// Add images sizes. function custom_theme_setup() { add_image_size( 'custom-size-1', 1200, 300, true ); add_image_size( 'custom-size-2', 1000, 333, true ); } add_action( 'after_setup_theme', 'custom_theme_setup' ); // Make custom sizes selectable from WordPress admin. function custom_image_sizes( $size_names ) { $new_sizes = array( 'custom-size-1' => __( 'Custom Size #1', 'generatewp.com' ), 'custom-size-2' => __( 'Custom Size #2', 'generatewp.com' ), ); return array_merge( $size_names, $new_sizes ); } add_filter( 'image_size_names_choose', 'custom_image_sizes' );Skip to note 4 content
Steven Lin
Example migrated from Codex:
The following will add a new image size option to the list of selectable sizes in the Media Library.
add_filter( 'image_size_names_choose', 'my_custom_sizes' ); function my_custom_sizes( $sizes ) { return array_merge( $sizes, array( 'your-custom-size' => __('Your Custom Size Name'), ) ); }