输出随机(自定义)图像大小

时间:2015-09-09 作者:p2or

使用自定义图像大小时,如:

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方法输出随机图像大小?

谢谢

1 个回复
最合适的回答,由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();
如果要排除某些图像大小,例如。thumbnailmedium 然后您可以使用:

/**
 * 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\' ] );

相关推荐

Remove <p></p> after images

我对<p></p> 出现在my之后的标记<img....>. 以下是我在本地主机上查看生成的页面时显示的内容。。。<img src=\"/wp-content/themes/wunderful/assets/images/feedback-danielle.png\" alt=\"Danielle Johnson Deal Town FC Treasurer\"> <p></p> 请注意随机生成的<p>&