使用自定义图像大小时,如:
function customImageSetup () {
add_theme_support( \'post-thumbnails\' );
add_image_size(\'grid_1 mini square\', 60, 60, TRUE);
add_image_size(\'grid_2\', 160);
add_image_size(\'grid_2 square\', 160, 160, TRUE);
add_image_size(\'grid_4\', 360);
add_image_size(\'grid_4 square\', 360, 360, TRUE);
add_image_size(\'grid_6\', 560);
add_image_size(\'grid_6 square\', 560, 560, TRUE);
add_image_size(\'grid_8\', 760);
add_image_size(\'grid_8 square\', 760, 760, TRUE);
add_image_size(\'grid_10\', 960);
add_image_size(\'grid_12\', 1160, FALSE);
}
是否有内置wordpress方法输出随机图像大小?
谢谢
最合适的回答,由SO网友:birgire 整理而成
我认为WordPress核心中不存在这样的功能。
示例#1
但您可以尝试以下操作:
/**
* Random image size
* @return String Image size
*/
function wpse_random_image_size()
{
$sizes = (array) get_intermediate_image_sizes();
return $sizes[array_rand( $sizes )];
}
显示随机图像大小。
Usage:
if ( function_exists( \'wpse_random_image_size\' ) )
echo wpse_random_image_size();
如果要排除某些图像大小,例如。
thumbnail
和
medium
然后您可以使用:
/**
* Random image size
* @param Array | String Sizes to exclude
* @return String Image size
*/
function wpse_random_image_size( $exclude = \'\' )
{
$sizes = array_diff( (array) get_intermediate_image_sizes(), (array) $exclude );
return $sizes[array_rand( $sizes )];
}
Usage:
if ( function_exists( \'wpse_random_image_size\' ) )
echo wpse_random_image_size( $exclude = [\'thumbnail\', \'medium\' ] );