这里我为您编写了一个函数。将以下功能放置在functions.php
或执行并将宽度和高度值传递给它的任何位置。它将返回图像大小名称string
如果没有找到,它就会返回false
.
/**
* Get name by size information for image.
*
* @global $_wp_additional_image_sizes
* @uses get_intermediate_image_sizes()
* @return string $_sizes bool Data for passed measurements image.
*/
function wpse_243461_get_image_size_name( $w = 150, $h = 150 ) {
global $_wp_additional_image_sizes;
$sizes = array();
foreach ( get_intermediate_image_sizes() as $_size ) {
if ( in_array( $_size, array(\'thumbnail\', \'medium\', \'medium_large\', \'large\') ) ) {
if ( $w == get_option( "{$_size}_size_w" ) && $h == get_option( "{$_size}_size_h" ))
return $_size;
} elseif ( isset( $_wp_additional_image_sizes[ $_size ] ) ) {
if ( $w == $_wp_additional_image_sizes[ $_size ][\'width\'] && $h == $_wp_additional_image_sizes[ $_size ][\'height\'] )
return $_size;
}
}
return false;
}
默认情况下,宽度为150,高度为150,传递方式如下
$w = 150, $h = 150
. 你必须像这样使用它
the_dramatist_get_image_size_name(300, 300)
. 请记住
$w
和
$h
采用整数值。